Kerry_Cui @ 2024-12-21 21:00:48
#include <bits/stdc++.h>
using namespace std;
int n, m,ans = 0;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
char a[1010][1010];
bool vis[1010][1010];//标记某个位置是否走过
void dfs(int x, int y) { //当前在x,y这个位置
if (x == 1 && y == n) {
ans++;
return;
}
for (int i = 0; i < 4; i++) {//枚举x,y的4个方位
int nx = x + dx[i], ny = y + dy[i];//周围的坐标
if (nx < 1 || nx > n || ny < 1 || ny > n) {
continue;//越界
}
if (vis[nx][ny]) {
continue;//已经走过了
}
if (a[nx][ny] == a[x][y]) {
continue;//不能走
}
vis[nx][ny] = 1;
dfs(nx, ny);
vis[nx][ny] = 0;
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
}
}
while(m--){
int x,y;
cin>>x>>y;
ans = 1;
dfs(x,y);
cout<<ans<<endl;
}
return 0;
}
by Ethan_gong @ 2024-12-22 08:36:26
if (x == 1 && y == n) {
ans++;
return;
}
这些是什么意思