EOJ:Frogger

Frogger

Time Limit: 1000MS Memory Limit: 65536K
Total Submits: 23 Accepted: 5

Description

Philip J. Frog just wanted to go for a mid-afternoon swim, but in typical frog fashion he's ended up in the middle of a busy street. Help Phil figure out how long he\'ll be hopping on hot asphalt before he finds his way to the nice cool water. 

Phil may hop one square horizontally or vertically per second. He may only hop onto road, grass, or water. Additionally, he cannot occupy any square occupied by a car. Phil and the cars move at the same time, meaning Phil can "hop over" an oncoming car. Phil can also remain in the same square if he wishes. All horizontal movement wraps (e.g., a rightward hop from the rightmost column places Phil in the leftmost column). Cars move horizontally in the direction indicated on the map ('<' means leftward, '>' means rightward) at a rate of one square per second and never collide with anything.

 

Input

Input begins with a single integer specifying the number of test maps. Each map begins with two integers R and C (0 < R, C ≤ 30) specifying the number of rows and columns, respectively, followed by R lines each C characters long, specifying the map. The possible map characters are: 

Phil ('&') - Phil's starting location. Each map contains exactly one. Always indicates road underneath. 
Tree ('T') - Impassable. 
Grass ('.') - Phil can move freely in the grass. 
Road ('-') - Hot! 
Car ('<', '>') - Always indicates road underneath. 
Water ('~') - Phil's goal.

 

Output

For each map, output a line containing the fewest number of seconds Phil must spend on the road in order to reach the water, or the string "Impassable", if no path to water exists.

 

Sample Input

3
2 1
~
&
4 7
~TTTTTT
.------
-->-<--
---&---
3 5
~~~~~
..T..
>>&<<

 

Sample Output

1
6
Impassable

原题:http://202.120.106.94/onlinejudge/problemshow.php?pro_id=106

——————————————————————————————————————————————————————————————

搜索题。

本题关键在于汽车会动!仔细读题发现,汽车开到边界会到另外一边,类似吃豆人。那么走过C步以后,地图和C步之前的一样,即地图一共只有C副,每走C步一个循环。

因此我们可以预先把地图处理好,然后进行搜索。

需要注意的是 

在草地上由于不计算耗费, 遍历过的状态有可能要更新, 因此, 不能遇到终点就跳出, 得全搜
另外还要注意,涉及到求余,数组下标最好从0开始!!!
代码
1 #include<stdio.h>
2 #include<memory.h>
3 #include<queue>
4  #define maxn 40
5  using namespace std;
6  int dx[5]={1,-1,0,0,0},
7 dy[5]={0,0,1,-1,0};
8  char str[maxn][maxn];
9  int map[maxn][maxn][maxn],hash[maxn][maxn][maxn];
10 int i,j,k,r,c,temp,ans,tt;
11 struct node
12 {
13 int x,y,state;
14 }t,p;
15 void init()
16 {
17 memset(map,0,sizeof(map));
18 scanf("%d%d",&r,&c);
19 for (i=0;i<r;i++)
20 {
21 getchar();
22 for (j=0;j<c;j++)
23 {
24 scanf("%c",&str[i][j]);
25 if (str[i][j]=='&')
26 {
27 t.x=i;
28 t.y=j;
29 }
30 else
31 switch (str[i][j])
32 {
33 case '<':map[i][j][0]=-1;break;
34 case '>':map[i][j][0]=1;break;
35 case 'T':map[i][j][0]=2;break;
36 case '.':map[i][j][0]=3;break;
37 case '~':map[i][j][0]=4;break;
38 }
39 }
40 }
41 for (k=1;k<c;k++)
42 for (i=0;i<r;i++)
43 for (j=0;j<c;j++)
44 {
45 if (map[i][j][0]==-1)
46 {
47 temp=(j-k+c)%c;
48 map[i][temp][k]=-1;
49 }
50 else
51 if (map[i][j][0]==1)
52 {
53 temp=(j+k)%c;
54 map[i][temp][k]=1;
55 }
56 if (map[i][j][0]>1)
57 map[i][j][k]=map[i][j][0];
58 }
59 }
60 void bfs()
61 {
62 queue<node> q;
63 t.state=0;
64 q.push(t);
65 memset(hash,0x1f,sizeof(hash));
66 hash[t.x][t.y][0]=0;
67 ans=0xffffff;
68 while (!q.empty())
69 {
70 t=q.front();
71 q.pop();
72 for (i=0;i<5;i++)
73 {
74 p.x=t.x+dx[i];
75 p.y=t.y+dy[i];
76 if (p.x<0||p.x>=r||p.y<0||p.y>=c) continue;
77 p.state=(t.state+1)%c;
78 if (map[p.x][p.y][p.state]==1||map[p.x][p.y][p.state]==-1||map[p.x][p.y][p.state]==2)
79
80 continue;
81 if (map[p.x][p.y][p.state]==4)
82 {
83 temp=hash[t.x][t.y][t.state]+1;
84 if (temp<ans) ans=temp;
85 }
86 if (map[p.x][p.y][p.state]==3)
87 temp=hash[t.x][t.y][t.state];
88 else temp=hash[t.x][t.y][t.state]+1;
89 if (hash[p.x][p.y][p.state]>temp)
90 {
91 hash[p.x][p.y][p.state]=temp;
92 q.push(p);
93 }
94 }
95 }
96 }
97 int main()
98 {
99 scanf("%d",&tt);
100 while (tt--)
101 {
102 init();
103 bfs();
104 if (ans<0xffffff)
105 printf("%d\n",ans);
106 else printf("Impassable\n");
107 }
108 return 0;
109 }

 


posted on 2011-02-04 20:38  风也轻云也淡  阅读(162)  评论(0编辑  收藏  举报