结构体对象做参数,会比单独做参数要慢些,我还傻傻的TLE几十遍。。。。。。。。涨记性了。
另外,本体的考点应开始剪枝。。。
只有我这样的奇葩因为用结构体对象而超时。。。
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
typedef struct{
int x;
int y;
int T;
}point;
point start, door;
int maze[8][8], vis[8][8];
int N, M, T;
bool dfs(int x, int y, int t)
{
if(x == door.x && y == door.y){
if(t == T)
return true;
else
return false;
}
int dis = abs(door.x - x) + abs(door.y - y);
if((T-t-dis)%2)
return false;
if(dis + t > T){
return false;
}
vis[x][y] = 1;
for(int i = 0; i < 4; i++){
if(vis[x+dir[i][0]][y+dir[i][1]] || !maze[x+dir[i][0]][y+dir[i][1]] || (x+dir[i][0] < 0 || x+dir[i][0] >= N) || (y+dir[i][1] < 0 || y+dir[i][1] >= M))
continue;
if(dfs(x+dir[i][0], y+dir[i][1], t+1))
return true;
}
vis[x][y] = 0;
return false;
}
int main()
{
while(1){
cin >> N >> M >> T;
if(!N || !M || !T) break;
memset(vis, 0, sizeof(vis));
memset(maze, 0, sizeof(maze));
char temp;
int wall = 0;
for(int i = 0; i < N; i++)
for(int j = 0; j < M; j++){
cin >> temp;
if(temp == '.'){
maze[i][j] = 1;
}
else if(temp == 'S'){
start.x = i; start.y = j;
}else if(temp == 'D'){
door.x = i; door.y = j;
maze[i][j] = 1;
}
}
bool answer = false;
vis[start.x][start.y] = 1;
answer = dfs(start.x , start.y, 0);
if(answer)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}