web123456

Pacific Atlantic Ocean Current Issues

  • #include<iostream>
  • using namespace std;
  • int height[201][201], book[201][201];
  • int m, n,num=1;
  • //Current point coordinates
  • void dfs(int x, int y) {
  • int i, j, k, tx, ty;
  • //Define four directions
  • int next[4][2] = {
  • {0,1},{1,0},{0,-1},{-1,0}
  • };//Lower right upper left
  • //Try to see if you can go in four directions
  • for (k = 0;k < 4;k++) {
  • tx = x + next[k][0];
  • ty = y + next[k][1];
  • //If the right point is greater than the current point, you can flow through and then determine whether the next point meets the condition. If you find the book that meets the condition, you can set it
  • if (tx>=0 && ty>=0 && tx<n && ty<m &&height[tx][ty] >= height[x][y]&&book[tx][ty]==0) {
  • book[tx][ty] =1;
  • dfs(tx, ty);
  • }
  • }
  • return;
  • }
  • int main() {
  • int i, j, a[201][201] = { 0 }, b[201][201];
  • //Enter rows and columns
  • cin >> n >> m;
  • //Enter data
  • for (i = 0;i < n;i++)
  • for (j = 0;j < m;j++)
  • cin >> height[i][j];
  • //Points that meet the conditions enter search (the points around are divided into four parts)
  • //superior
  • for (i = 0;i < n;i++) {
  • book[0][i] = 1;
  • dfs(0, i);
  • }
  • //Left
  • for (i = 1;i < n;i++){
  • book[i][0] = 1;
  • dfs(i, 0);}
  • for (i = 0;i < n;i++)
  • for (j = 0;j < m;j++)
  • a[i][j] = book[i][j];//Assign all the passing through the book to the a array and temporarily save it
  • //Clear the book tag array
  • for (i = 0;i < n;i++) {
  • for (j = 0;j < m;j++)
  • book[i][j] = { 0 };
  • }
  • cout << endl;
  • //Down
  • for (i = 1;i < n;i++) {
  • book[4][i] = 1;
  • dfs(n-1, i);
  • }
  • //right
  • for (i = 1;i < n - 1;i++) {
  • book[i][4] = 1;
  • dfs(i, n-1);
  • }
  • //Find the overlapping part of a and book array for output
  • for (i = 0;i < n;i++)
  • for (j = 0;j < m;j++)
  • {
  • if (a[i][j] == book[i][j])
  • cout << i << " " << j << endl;
  • }
  • return 0;
  • }
  • 5 5
  • 1 2 2 3 5
  • 3 2 3 4 4
  • 2 4 5 3 1
  • 6 7 1 4 5
  • 5 1 1 2 4