一开始的思路是,先走驴子,之后走老虎,每次走的时候记录他们的达到该位置的步数,如果一样就可以相遇,特殊处理末尾。
但是没办法过,之后索性让他们一起走,一起修改移动的坐标,结果过了。。。
真心不知道错哪了。。
11662502 | 2014-09-16 00:43:01 | Accepted | 4740 | 31MS | 8300K | 2547 B | G++ | KinderRiven |
#include<cstdio> #include<cstring> #include<cstdlib> #include<iostream> #include<string> #include<vector> #include<stack> #include<set> #include<algorithm> #include<map> #include<sstream> #include<cmath> #include<queue> #define INF (1 << 30) #define eps (1e-10) #define _PI acos(-1.0) using namespace std; /*========================================= =========================================*/ #define MAXD 1000 + 10 int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}}; //¶« ÄÏ Î÷ ±± int n; int dist1[MAXD][MAXD]; int dist2[MAXD][MAXD]; int ans_x,ans_y; void dfs_d(int x,int y,int d,int dist){ dist1[x][y] = dist; int _x = x; int _y = y; int _dist = dist; while(true){ int t_x = _x; int t_y = _y; _x = _x + dir[d][0]; _y = _y + dir[d][1]; if(_x >= 0 && _x < n && _y >=0 && _y < n && dist1[_x][_y] == -1){ _dist ++; dist1[_x][_y] = _dist; } else{ _x = t_x; _y = t_y; d = (d + 1) % 4; _x = _x + dir[d][0]; _y = _y + dir[d][1]; if(_x >= 0 && _x < n && _y >= 0 && _y < n && dist1[_x][_y] == -1){ dfs_d(_x,_y,d,_dist + 1); return ; } else{ return ; } } } } void dfs_t(int x,int y,int d,int dist){ dist2[x][y] = dist; int _x = x; int _y = y; int _dist = dist; while(true){ int t_x = _x; int t_y = _y; _x = _x + dir[d][0]; _y = _y + dir[d][1]; if(_x >= 0 && _x < n && _y >=0 && _y < n && dist2[_x][_y] == -1){ _dist ++; dist2[_x][_y] = _dist; } else{ _x = t_x; _y = t_y; d = (d + 3) % 4; _x = _x + dir[d][0]; _y = _y + dir[d][1]; if(_x >= 0 && _x < n && _y >= 0 && _y < n && dist2[_x][_y] == -1){ dfs_t(_x,_y,d,_dist + 1); return ; } else{ return ; } } } } int main(){ while(scanf("%d",&n) && n){ int x , y, d; memset(dist1,-1,sizeof(dist1)); memset(dist2,-1,sizeof(dist2)); scanf("%d%d%d",&x,&y,&d); dfs_d(x,y,d,0); scanf("%d%d%d",&x,&y,&d); dfs_t(x,y,d,0); int ans_dist = INF; for(int i = 0 ; i < n ; i++){ for(int j = 0 ; j < n ; j++) printf("%d ",dist1[i][j]); printf("\n"); } for(int i = 0 ; i < n ; i++) for(int j = 0 ; j < n ; j++)if(dist1[i][j] != -1 && dist2[i][j] != -1){ if(dist1[i][j] == dist2[i][j]){ if(ans_dist > dist1[i][j]){ ans_x = i; ans_y = j; ans_dist = dist1[i][j]; } } } if(ans_dist < INF) printf("%d %d\n",ans_x,ans_y); else printf("-1\n"); } return 0; }
时间: 2024-10-26 13:51:15