Title Link: http://poj.org/problem?id=2312
The main topic: Give a n*m matrix, where Y is the starting point, T is the end point, B and E can go, s and R can not go, to note is to walk B takes 2 minutes, walk E takes a minute. The shortest time to solve y--->t.
See this question first think of the wide search to find the shortest time, but here can be dealt with B and E, convenient calculation ~
1 #include <iostream> 2 #include <cstdio> 3 #include <queue> 4 #include <cstring> 5 using Nam
Espace std;
6 int dir[4][2]= {1,0,-1,0,0,1,0,-1};
7 bool vis[310][310];
8 Char map[310][310];
9 int m,n,sx,sy; Ten struct node one {int x,y,time;--friend bool operator< (node A,node b)--{return a.time&
Gt;b.time;
16} 17}; () () () () () () () () (+) (s,ss,sss), priority_queue<node>q;, S.X=SX, S.tim
e=0;
Q.push (s);
Vis[sx][sy]=1; while (!q.empty ()), {ss=q.top (); Q.pop (); (int i=0; i<4; i++) 33 {sss.x=ss.x+dir[i][0]; sss.y=ss.y+dir[i][1]; if (sss.x<0| | sss.y<0| | sss.x>=n| | sss.y>=m| | map[sss.x][sss.y]== ' R ' | | map[sss.x][sss.y]== ' S ' | |
VIS[SSS.X][SSS.Y]) Panax Notoginseng continue; if (map[sss.x][sss.y]== ' B ') sss.time=ss.time+2;
sss.time=ss.time+1;
//sss.time=ss.time+1;
if (map[sss.x][sss.y]== ' T '), return sss.time;
Vis[sss.x][sss.y]=1;
Q.push (SSS);
(return-1); () [~scanf ("%d%d", &n,&m)) (+ 57) (+)
Break
memset (vis,0,sizeof (VIS)); (int i=0; i<n; i++) {GetChar (); (int j=0; j<m; j + +) 63 {scanf ("%c", &map[i][j]), if (map[i][j]== ' Y ') 66 {6
7 sx=i;
Sy=j;
("%d\n", BFS ());
0; 75}
View Code
There is a kind of, simple wide search is also possible.
1 #include <iostream> 2 #include <cstdio> 3 #include <queue> 4 #include <cstring> 5 using Nam
Espace std;
6 int dir[4][2]= {1,0,-1,0,0,1,0,-1};
7 bool vis[310][310];
8 Char map[310][310];
9 int m,n,sx,sy;
Ten struct Node {x,y,time; 13}; +-int BFS () () () () (S,SS,SSS),//priority_queue<node>q; queue<node>q;
;
S.y=sy;
s.time=0;
Q.push (s);
Vis[sx][sy]=1; 30 while (!q.empty ())-Ss=q.front (); Q.pop (); for (int i=0; i<4; i++ {sss.x=ss.x+dir[i][0]; sss.y=ss.y+dir[i][1]; if (sss.x<0| | sss.y<0| | sss.x>=n| | sss.y>=m| | map[sss.x][sss.y]== ' R ' | | map[sss.x][sss.y]== ' S ' | |
VIS[SSS.X][SSS.Y]) continue;
if (map[sss.x][sss.y]== ' B ') sss.time=ss.time+2; Notoginseng else sss.time=ss.time+1;
//sss.time=ss.time+1;
if (map[sss.x][sss.y]== ' T '), return sss.time;
Vis[sss.x][sss.y]=1;
Q.push (SSS);
Return-1}; () (~SCANF ("%d%d", &n,&m)) (54) (n==0&&m==0).
Break
memset (vis,0,sizeof (VIS)); 60 for (int i=0; i<n; i++) j=0 {GetChar (); (int j<m;; j + +) {scanf ("%c", &map[i][j]), if (map[i][j]== ' Y ') 63 {6
4 sx=i;
Sy=j;
("%d\n", BFS ());
0}; 72}
View Code