标签:dfs
1 5 5 14 S*#*. .#... ..... ****. ...#. ..*.P #.*.. ***.. ...*. *.#..
YES#include <iostream> #include <cstring> #include <cstdio> #include <cmath> using namespace std; char str[2][20][20]; int len[2][20][20]; //int tp[] = {1, 0, , -1, 0}; int xx[4][2] = {{1, 0}, {0, 1}, { -1, 0}, {0, -1}}; int s, t, n, m; int x, y, q; bool flag; bool bfs(int a, int b, int C, int temp) { int ax, ay, c; //cout << C << " " << a - 1 << " " << b - 1 << " " << temp << endl; //getchar(); if (str[C][a][b] == 'P') { //flag=true; return 1; } if (temp <= 0)return 0; int sm = abs(a - x) + abs(b - y); if (sm > temp)return 0; for (int i = 0; i < 4; i++) { ax = a + xx[i][0]; ay = b + xx[i][1]; c = C; if (ax >= 1 && ax <= n && ay >= 1 && ay <= m && !len[c][ax][ay] && str[c][ax][ay] != '*') { if (str[c][ax][ay] == '#') { c = 1 - c; } len[c][ax][ay] = 1; if (str[c][ax][ay] != '#' && str[c][ax][ay] != '*'&& bfs(ax, ay, c, temp - 1)) { return 1; } len[c][ax][ay] = 0; } } return 0; } int main() { cin >> s; while (s--) { flag=false; memset(len, 0, sizeof(len)); cin >> n >> m >> t; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { cin >> str[0][i][j]; if (str[0][i][j] == 'P') { x = i; y = j; q = 0; } } for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { cin >> str[1][i][j]; if (str[1][i][j] == 'P') { x = i; y = j; q = 1; } } if(bfs(1, 1, 0, t)) cout << "YES" << endl; else cout << "NO" << endl; } return 0; }
标签:dfs
原文地址:http://blog.csdn.net/zhangweiacm/article/details/37913895