【发布时间】:2017-01-08 15:28:06
【问题描述】:
http://codeforces.com/contest/540/problem/C
使用矩阵上的深度优先递归解决了这个问题,我得到了这个问题的代码。谁能解释为什么/在哪里递归在这个矩阵上起作用?
#include <bits/stdc++.h>
using namespace std;
int a, b, used[600][600];
char c;
void dfs( int x, int y )
{
used[x][y]++;
if( used[x][y] >= 3 || x < 1 || y < 1 || x > a || y > b )
return;
dfs(x + 1, y);
dfs(x - 1, y);
dfs(x, y + 1);
dfs(x, y - 1);
}
int main()
{
int i, j;
cin >> a >> b;
for( i = 1; i <= a; i++ ){
for( j = 1; j <= b; j++ ){
cin >> c;
if( c == '.' ) used[i][j] = 1;
else used[i][j] = 2;
}
}
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
used[x1][y1] = 1;
dfs(x1, y1);
if( used[x2][y2] >= 3 ) cout << "YES";
else cout << "NO";
}
【问题讨论】: