搜索

1010

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<cmath>
 5 
 6 using namespace std;
 7 
 8 int n,m,time,ok,dir[][5]={{1,0},{-1,0},{0,1},{0,-1}},dx,dy;
 9 char g[200][200];
10 
11 void dfs(int x,int y,int t)
12 {
13     if(t==time) {if(x==dx&&y==dy) ok=1; return;}
14     if(ok) return;
15     int temp=abs(x-dx)+abs(y-dy)-abs(t-time);
16     if(temp>0||temp&1) return;
17     for(int i=0;i<4&&!ok;i++)
18     {
19         int x1,y1;
20         x1=x+dir[i][0];
21         y1=y+dir[i][1];
22         if(x1>0&&x1<=n&&y1>0&&y1<=m)
23         {
24             if(g[x1][y1]!='X')
25             {
26                 g[x][y]='X';
27                 dfs(x1,y1,t+1);
28                 g[x][y]='.';
29             }
30         }
31     }
32 }
33 
34 int main()
35 {
36     while(cin>>n>>m>>time&&(n||m||time))
37     {
38         int x,y;
39         ok=0;
40         for(int i=1;i<=n;i++)
41             for(int j=1;j<=m;j++)
42         {
43             cin>>g[i][j];
44             if(g[i][j]=='S')
45                 x=i,y=j;
46             if(g[i][j]=='D')
47                 dx=i,dy=j;
48         }
49         dfs(x,y,0);
50         if(ok) cout<<"YES"<<endl;
51         else cout<<"NO"<<endl;
52     }
53 }
View Code

 1241

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 using namespace std;
 5 
 6 int n,m,mark[200][200],dir[][5]={{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};
 7 char g[200][200];
 8 
 9 void dfs(int x,int y,int cnt)
10 {
11     mark[x][y]=cnt;
12     for(int i=0;i<8;i++)
13     {
14         int x1,y1;
15         x1=x+dir[i][0],y1=y+dir[i][1];
16         if(x1>=0&&x1<n&&y1>=0&&y1<m)
17         {
18             if(g[x1][y1]=='@'&&!mark[x1][y1])
19                 dfs(x1,y1,cnt);
20         }
21     }
22 }
23 
24 int main()
25 {
26     while(cin>>n>>m&&(n||m))
27     {
28         for(int i=0;i<n;i++) cin>>g[i];
29         int id=0;
30         memset(mark,0,sizeof(mark));
31         for(int i=0;i<n;i++)
32             for(int j=0;j<m;j++)
33         {
34             if(!mark[i][j])
35                 if(g[i][j]=='@')
36                     dfs(i,j,++id);
37         }
38         cout<<id<<endl;
39     }
40 }
View Code

 1312

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<queue>
 5 using namespace std;
 6 #define AA struct ss
 7 
 8 int n,m,cnt,dir[][5]={{0,1},{0,-1},{1,0},{-1,0}};
 9 char g[100][100];
10 AA{
11 int a,b;
12 };
13 
14 void bfs(int x,int y)
15 {
16     AA k;
17     k.a=x,k.b=y;
18     queue<AA>que;
19     que.push(k);
20     while(!que.empty())
21     {
22         k=que.front();
23         que.pop();
24         for(int i=0;i<4;i++)
25         {
26             AA p;
27             p.a=k.a+dir[i][0];
28             p.b=k.b+dir[i][1];
29             if(p.a>=0&&p.a<n&&p.b>=0&&p.b<m)
30             {
31                 if(g[p.a][p.b]=='.')
32                 {
33                     cnt++;
34                     que.push(p);
35                     g[p.a][p.b]='#';
36                 }
37             }
38         }
39     }
40 }
41 
42 int main()
43 {
44     while(cin>>m>>n&&(n||m))
45     {
46         int x,y;
47         cnt=1;
48         for(int i=0;i<n;i++)
49             for(int j=0;j<m;j++)
50         {
51             cin>>g[i][j];
52             if(g[i][j]=='@') x=i,y=j;
53         }
54         bfs(x,y);
55         cout<<cnt<<endl;
56     }
57 }
View Code

 1242

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<queue>
 5 using namespace std;
 6 #define AA struct ss
 7 
 8 int n,m,num,dir[][5]={{0,1},{0,-1},{1,0},{-1,0}};
 9 char g[205][205];
10 
11 AA
12 {
13     int x,y,t;
14     friend bool operator < (ss a, ss b)
15     {
16         return a.t>b.t;
17     }
18 };
19 
20 int bfs(int x1,int y1)
21 {
22     priority_queue<ss>que;
23     AA k;
24     g[x1][y1]='#';
25     k.x=x1;k.y=y1;k.t=0;
26     que.push(k);
27     while(!que.empty())
28     {
29         k=que.top();
30         que.pop();
31         for(int i=0;i<4;i++)
32         {
33             AA p;
34             p.x=k.x+dir[i][0];
35             p.y=k.y+dir[i][1];
36             p.t=k.t+1;
37             if(p.x>=0&&p.x<n&&p.y>=0&&p.y<m)
38             {
39                 if(g[p.x][p.y]!='#')
40                 {
41                     if(g[p.x][p.y]=='x') p.t++;
42                     if(g[p.x][p.y]=='r') return p.t;
43                     que.push(p);
44                     g[p.x][p.y]='#';
45                 }
46             }
47         }
48     }
49     return -1;
50 }
51 
52 int main()
53 {
54     while(cin>>n>>m)
55     {
56         int x1,y1;
57         for(int i=0;i<n;i++)
58             for(int j=0;j<m;j++)
59         {
60             cin>>g[i][j];
61             if(g[i][j]=='a')
62                 x1=i,y1=j;
63             if(g[i][j]=='x') num++;
64         }
65         int f=bfs(x1,y1);
66         if(f!=-1) cout<<f<<endl;
67         else cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
68     }
69 }
View Code

 1026

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<iostream>
  4 #include<queue>
  5 using namespace std;
  6 
  7 #define AA struct ss
  8 #define BB struct sss
  9 
 10 AA
 11 {
 12     int x,y,t;
 13     friend bool operator < (ss a,ss b)
 14     {
 15         return a.t>b.t;
 16     }
 17 };
 18 
 19 BB
 20 {
 21     char c;
 22     int nx,ny;
 23 }g[105][105];
 24 
 25 int n,m,fig[105][105],dir[][5]={{1,0},{-1,0},{0,1},{0,-1}};
 26 
 27 int bfs()
 28 {
 29     priority_queue<AA>que;
 30     AA k;
 31     k.x=n-1,k.y=m-1,k.t=0;
 32     if(isdigit(g[n-1][m-1].c))
 33     {
 34         fig[n-1][m-1]=g[n-1][m-1].c-'0';
 35         k.t+=fig[n-1][m-1];
 36     }
 37     que.push(k);
 38     g[k.x][k.y].c='X';
 39     while(!que.empty())
 40     {
 41         k=que.top();
 42         que.pop();
 43         if(k.x==0&&k.y==0) return k.t;
 44         for(int i=0;i<4;i++)
 45         {
 46             AA p;
 47             p.x=k.x+dir[i][0];
 48             p.y=k.y+dir[i][1];
 49             p.t=k.t+1;
 50             if(p.x>=0&&p.x<n&&p.y>=0&&p.y<m&&g[p.x][p.y].c!='X')
 51             {
 52                 g[p.x][p.y].nx=k.x;
 53                 g[p.x][p.y].ny=k.y;
 54                 if(isdigit(g[p.x][p.y].c))
 55                 {
 56                     fig[p.x][p.y]=g[p.x][p.y].c-'0';
 57                     p.t+=fig[p.x][p.y];
 58                 }
 59                 que.push(p);
 60                 g[p.x][p.y].c='X';
 61             }
 62         }
 63     }
 64     return -1;
 65 }
 66 
 67 int main()
 68 {
 69     while(cin>>n>>m)
 70     {
 71         for(int i=0;i<n;i++)
 72             for(int j=0;j<m;j++)
 73             cin>>g[i][j].c;
 74             memset(fig,0,sizeof(fig));
 75         int ans=bfs();
 76         if(ans==-1)
 77         {
 78             cout<<"God please help our poor hero."<<endl;
 79             cout<<"FINISH"<<endl;
 80         }
 81         else {
 82             cout<<"It takes "<<ans<<" seconds to reach the target position, let me show you the way."<<endl;
 83             int cnt=1,x=0,y=0;
 84             if(fig[x][y])
 85             {
 86                 for(int i=0;i<fig[x][y];i++) printf("%ds:FIGHT AT (%d,%d)\n",cnt++,x,y);
 87             }
 88             while(cnt<=ans)
 89             {
 90                 int fx=g[x][y].nx;int fy=g[x][y].ny;
 91                 printf("%ds:(%d,%d)->(%d,%d)\n",cnt++,x,y,fx,fy);
 92                 if(fig[fx][fy])
 93                 {
 94                     for(int i=0;i<fig[fx][fy];i++) printf("%ds:FIGHT AT (%d,%d)\n",cnt++,fx,fy);
 95                 }
 96                 x=fx,y=fy;
 97             }
 98             cout<<"FINISH"<<endl;
 99         }
100     }
101 }
View Code

 1072

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<queue>
 5 #define AA struct ss
 6 using namespace std;
 7 
 8 int n,m,g[10][10],cnt,dir[][5]={{1,0},{-1,0},{0,1},{0,-1}};
 9 AA{
10     char G[10][10];
11     int x,y,t,time;
12 };
13 
14 int bfs(int r,int c)
15 {
16     queue<AA>que;
17     AA k;
18     memset(k.G,0,sizeof(k.G));
19     k.x=r,k.y=c;
20     k.t=6;k.time=0;
21     g[k.x][k.y]=0;
22     que.push(k);
23     while(!que.empty())
24     {
25         k=que.front();
26         que.pop();
27         if(k.t==1) continue;
28         for(int i=0;i<4;i++)
29         {
30             AA p;
31             p=k;
32             p.x=k.x+dir[i][0];
33             p.y=k.y+dir[i][1];
34             p.t=k.t-1;
35             p.time=k.time+1;
36             if(p.x>=0&&p.x<n&&p.y>=0&&p.y<m&&g[p.x][p.y]!=0&&!p.G[p.x][p.y])
37             {
38                 p.G[p.x][p.y]=1;
39                 if(g[p.x][p.y]==3) return p.time;
40                 if(g[p.x][p.y]==4) {p.t=6;g[p.x][p.y]=0;memset(p.G,0,sizeof(p.G));}
41                 que.push(p);
42             }
43         }
44     }
45     return -1;
46 }
47 
48 int main()
49 {
50     int T;
51     cin>>T;
52     while(T--)
53     {
54         int x,y;
55         cin>>n>>m;
56         for(int i=0;i<n;i++)
57             for(int j=0;j<m;j++)
58         {
59             cin>>g[i][j];
60             if(g[i][j]==2)
61                 x=i,y=j;
62         }
63         cnt = bfs(x,y);
64         cout<<cnt<<endl;
65     }
66 }
View Code

 1175

 1 #include<cstring>
 2 #include<cstdio>
 3 #include<iostream>
 4 #include<queue>
 5 using namespace std;
 6 #define AA struct ss
 7 AA
 8 {
 9     int x,y,t,w;
10 };
11 
12 int n,m,g[1005][1005],vis[1005][1005],dir[][5]={{0,1},{0,-1},{1,0},{-1,0}};
13 
14 int bfs(int x1,int y1,int x2,int y2)
15 {
16     AA  k;
17     k.x=x1;k.y=y1;k.t=0;k.w=-1;
18     queue<AA>que;
19     que.push(k);
20     while(!que.empty())
21     {
22         k=que.front();
23         que.pop();
24         for(int i=0;i<4;i++)
25         {
26             AA p;
27             p.x=k.x+dir[i][0];
28             p.y=k.y+dir[i][1];
29             p.t=k.t;
30             p.w=i;
31             if(p.w!=k.w&&k.w!=-1) p.t+=1;
32             if(p.t==3) continue;
33             if(p.x>0&&p.x<=n&&p.y>0&&p.y<=m)
34             {
35                 if(!vis[p.x][p.y])
36                 {
37                     vis[p.x][p.y]=1;
38                     if(p.x==x2&&p.y==y2) {return 1;}
39                     if(!g[p.x][p.y]) que.push(p);
40                 }
41             }
42 
43         }
44     }
45     return -1;
46 }
47 
48 int main()
49 {
50     while(cin>>n>>m&&(n||m))
51     {
52         for(int i=1;i<=n;i++)
53             for(int j=1;j<=m;j++)
54             cin>>g[i][j];
55         int T;
56         cin>>T;
57         while(T--)
58         {
59             memset(vis,0,sizeof(vis));
60             int ok,x1,y1,x2,y2;
61             cin>>x1>>y1>>x2>>y2;
62             if(g[x1][y1]==0||g[x2][y2]==0||g[x1][y1]!=g[x2][y2]||(x1==x2&&y1==y2))
63             {
64                 ok=-1;
65             }
66             else {
67                ok=bfs(x1,y1,x2,y2);
68             }
69             if(ok!=-1) cout<<"YES"<<endl;
70             else cout<<"NO"<<endl;
71         }
72     }
73 
74 }
View Code

 1180

  1 #include<cstring>
  2 #include<cstdio>
  3 #include<iostream>
  4 #include<queue>
  5 using namespace std;
  6 #define AA struct ss
  7 
  8 int n,m,dir[][5]={{0,1},{0,-1},{1,0},{-1,0}};
  9 char g[25][25];
 10 AA
 11 {
 12     int x,y,t;
 13     friend bool operator < (AA a,AA b)
 14     {
 15         return a.t>b.t;
 16     }
 17 };
 18 
 19 int bfs(int r,int c)
 20 {
 21     g[r][c]='*';
 22     priority_queue<AA>que;
 23     AA k;
 24     k.x=r,k.y=c,k.t=0;
 25     que.push(k);
 26     while(!que.empty())
 27     {
 28         k=que.top();
 29         que.pop();
 30         //cout<<k.x<<' '<<k.y<<' '<<k.t<<endl;
 31         for(int i=0;i<4;i++)
 32         {
 33             AA p;
 34             p.x=k.x+dir[i][0];
 35             p.y=k.y+dir[i][1];
 36             p.t=k.t+1;
 37             if(p.x>=0&&p.x<n&&p.y>=0&&p.y<m)
 38             {
 39                 k.t+=2;
 40                 if(g[p.x][p.y]=='.')
 41                 {
 42                     g[p.x][p.y]='*';
 43                     que.push(p);
 44                 }
 45                 else if(g[p.x][p.y]=='0')
 46                 {
 47                     if(k.t%2==0)
 48                     {
 49                         if(i==2||i==3)
 50                         {
 51                            // g[p.x][p.y]='*';
 52                             p.x=p.x+dir[i][0];
 53                             p.y=p.y+dir[i][1];
 54                             p.t=p.t;
 55                             if(g[p.x][p.y]=='T') return p.t;
 56                             else if(g[p.x][p.y]=='.')
 57                             {
 58                                 g[p.x][p.y]='*';
 59                                 que.push(p);
 60                             }
 61                         }
 62                         else {
 63                             k.t-=1;
 64                             que.push(k);
 65                         k.t+=1;
 66                         }
 67                     }
 68                     else if(k.t%2)
 69                     {
 70                        if(i==0||i==1)
 71                         {
 72                           //  g[p.x][p.y]='*';
 73                             p.x=p.x+dir[i][0];
 74                             p.y=p.y+dir[i][1];
 75                             p.t=p.t;
 76                             if(g[p.x][p.y]=='T') return p.t;
 77                             else if(g[p.x][p.y]=='.')
 78                             {
 79                                 g[p.x][p.y]='*';
 80                                 que.push(p);
 81                             }
 82                         }
 83                        else {
 84                             k.t-=1;
 85                             que.push(k);
 86                         k.t+=1;
 87                         }
 88                     }
 89                 }
 90                 else if(g[p.x][p.y]=='1')
 91                 {
 92                     if(k.t%2==0)
 93                     {
 94                         if(i==0||i==1)
 95                         {
 96                           //  g[p.x][p.y]='*';
 97                             p.x=p.x+dir[i][0];
 98                             p.y=p.y+dir[i][1];
 99                             p.t=p.t;
100                             if(g[p.x][p.y]=='T') return p.t;
101                             else if(g[p.x][p.y]=='.')
102                             {
103                                 g[p.x][p.y]='*';
104                                 que.push(p);
105                             }
106                         }
107                         else {
108                             k.t-=1;
109                             que.push(k);
110                         k.t+=1;
111                         }
112                     }
113                     else if(k.t%2)
114                     {
115                        if(i==2||i==3)
116                         {
117                          //   g[p.x][p.y]='*';
118                             p.x=p.x+dir[i][0];
119                             p.y=p.y+dir[i][1];
120                             p.t=p.t;
121                             if(g[p.x][p.y]=='T') return p.t;
122                             else if(g[p.x][p.y]=='.')
123                             {
124                                 g[p.x][p.y]='*';
125                                 que.push(p);
126                             }
127                         }
128                         else {
129                             k.t-=1;
130                             que.push(k);
131                         k.t+=1;
132                         }
133                     }
134                 }
135                 else if(g[p.x][p.y]=='T') return p.t;
136                 k.t-=2;
137             }
138         }
139     }
140     return -1;
141 }
142 
143 int main()
144 {
145     while(cin>>n>>m)
146     {
147         int x,y;
148         for(int i=0;i<n;i++)
149             for(int j=0;j<m;j++)
150         {
151             cin>>g[i][j];
152             if(g[i][j]=='S') x=i,y=j;
153             if(g[i][j]=='|') g[i][j]='0';
154             if(g[i][j]=='-') g[i][j]='1';
155         }
156         int cnt=bfs(x,y);
157         cout<<cnt<<endl;
158     }
159 }
View Code

 1240

 1 #include<cstring>
 2 #include<cstdio>
 3 #include<iostream>
 4 #include<queue>
 5 using namespace std;
 6 
 7 #define AA struct ss
 8 
 9 AA{
10     int x,y,z,t;
11 };
12 
13 char g[15][15][15];
14 int x2,y2,z2,n,dir[][6]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
15 
16 bool judge(int x,int y,int z)
17 {
18     if(x>=0&&x<n&&y>=0&&y<n&&z>=0&&z<n) return true;
19     return false;
20 }
21 
22 int bfs(int a,int b,int c)
23 {
24     queue<AA>que;
25     AA k;
26     k.x=a,k.y=b,k.z=c,k.t=0;
27     que.push(k);
28     g[k.x][k.y][k.z]='X';
29     while(!que.empty())
30     {
31         k=que.front();
32         que.pop();
33         if(k.x==z2&&k.y==y2&&k.z==x2) return k.t;
34       //  cout<<k.x<<' '<<k.y<<' '<<k.z<<' '<<k.t<<endl;
35         for(int i=0;i<6;i++)
36         {
37             AA p=k;
38             p.x+=dir[i][0];
39             p.y+=dir[i][1];
40             p.z+=dir[i][2];
41             p.t+=1;
42             if(judge(p.x,p.y,p.z))
43             {
44                 if(g[p.x][p.y][p.z]=='O')
45                 {
46                     que.push(p);
47                     g[p.x][p.y][p.z]='X';
48                 }
49             }
50         }
51     }
52     return -1;
53 }
54 
55 int main()
56 {
57     string c;
58     while(cin>>c>>n)
59     {
60         for(int i=0;i<n;i++)
61             for(int j=0;j<n;j++)
62             cin>>g[i][j];
63         int x1,y1,z1;
64         cin>>x1>>y1>>z1>>x2>>y2>>z2;
65         cin>>c;
66    //     cout<<g[x2][y2][z2]<<' '<<g[x1][y1][z1]<<endl;
67         int ok=bfs(x1,y1,z1);
68         if(ok==-1) cout<<"NO ROUTE"<<endl;
69         else cout<<n<<' '<<ok<<endl;
70     }
71 }
View Code

 1253

 1 #include<cstring>
 2 #include<cstdio>
 3 #include<iostream>
 4 #include<queue>
 5 using namespace std;
 6 
 7 #define AA struct ss
 8 
 9 AA{
10     int x,y,z,t;
11 };
12 
13 int  g[55][55][55];
14 int x1,y1,z1,n,dir[][6]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
15 
16 bool judge(int a,int b,int c)
17 {
18     if(a>=0&&a<x1&&b>=0&&b<y1&&c>=0&&c<z1) return true;
19     return false;
20 }
21 
22 int bfs(int a,int b,int c)
23 {
24     queue<AA>que;
25     AA k;
26     k.x=a,k.y=b,k.z=c,k.t=0;
27     que.push(k);
28     g[k.x][k.y][k.z]=1;
29     while(!que.empty())
30     {
31         k=que.front();
32         que.pop();
33         if(k.x==x1-1&&k.y==y1-1&&k.z==z1-1) return k.t;
34       //  cout<<k.x<<' '<<k.y<<' '<<k.z<<' '<<k.t<<endl;
35         for(int i=0;i<6;i++)
36         {
37             AA p=k;
38             p.x+=dir[i][0];
39             p.y+=dir[i][1];
40             p.z+=dir[i][2];
41             p.t+=1;
42             if(judge(p.x,p.y,p.z))
43             {
44                 if(g[p.x][p.y][p.z]==0)
45                 {
46                     que.push(p);
47                     g[p.x][p.y][p.z]=1;
48                 }
49             }
50         }
51     }
52     return -1;
53 }
54 
55 int main()
56 {
57     int T;
58     scanf("%d",&T);
59     while(T--)
60     {
61         scanf("%d%d%d%d",&x1,&y1,&z1,&n);
62         for(int i=0;i<x1;i++)
63             for(int j=0;j<y1;j++)
64                 for(int k=0;k<z1;k++)
65             scanf("%d",&g[i][j][k]);
66         int ok=bfs(0,0,0);
67         if(ok<=n) printf("%d\n",ok);
68         else printf("-1\n");
69     }
70 }
View Code

 1372

 1 #include<cstring>
 2 #include<cstdio>
 3 #include<iostream>
 4 #include<queue>
 5 using namespace std;
 6 
 7 #define AA struct ss
 8 AA{
 9     int x,y,t;
10 };
11 
12 int x1,y1,vis[15][15],dir[][6]={{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}};
13 
14 int bfs(int a,int b)
15 {
16     queue<AA>que;
17     AA k;
18     k.x=a,k.y=b,k.t=0;
19     que.push(k);
20     while(!que.empty())
21     {
22         k=que.front();
23         que.pop();
24         if(k.x==x1&&k.y==y1) return k.t;
25         for(int i=0;i<8;i++)
26         {
27             AA p=k;
28             p.x+=dir[i][0];
29             p.y+=dir[i][1];
30             p.t+=1;
31 
32             if(p.x>0&&p.x<('h'-'a'+2)&&p.y>0&&p.y<9&&!vis[p.x][p.y])
33             {
34                 vis[p.x][p.y]=1;
35                 que.push(p);
36             }
37         }
38     }
39 
40 }
41 
42 int main()
43 {
44     char c[5],d[5];
45     while(cin>>c>>d)
46     {
47         memset(vis,0,sizeof(vis));
48         int x,y;
49         x=c[0]-'a'+1;
50         y=c[1]-'0';
51         x1=d[0]-'a'+1;
52         y1=d[1]-'0';
53 
54         int ok=bfs(x,y);
55         printf("To get from %s to %s takes %d knight moves.\n",c,d,ok);
56     }
57 }
View Code

 1548

 1 #include<cstring>
 2 #include<cstdio>
 3 #include<iostream>
 4 #include<queue>
 5 using namespace std;
 6 
 7 #define AA struct ss
 8 AA
 9 {
10     int x,t;
11 };
12 int n,a,b,num[205],vis[205];
13 
14 int bfs(int a)
15 {
16     queue<AA>que;
17     AA k;
18     k.x=a;k.t=0;
19     que.push(k);
20     while(!que.empty())
21     {
22         AA k=que.front();
23         que.pop();
24         if(k.x==b) return k.t;
25         AA p;
26         p.x=k.x+num[k.x];p.t=k.t+1;
27         if(p.x>0&&p.x<=n&&!vis[p.x])
28         {
29             vis[p.x]=1;
30             que.push(p);
31         }
32         p.x=k.x-num[k.x];p.t=k.t+1;
33         if(p.x>0&&p.x<=n&&!vis[p.x])
34         {
35             vis[p.x]=1;
36             que.push(p);
37         }
38     }
39     return -1;
40 }
41 
42 int main()
43 {
44     while(cin>>n&&n)
45     {
46         cin>>a>>b;
47         memset(vis,0,sizeof(vis));
48         for(int i=1;i<=n;i++) cin>>num[i];
49         int ok=bfs(a);
50         cout<<ok<<endl;
51     }
52 }
View Code

 1728

 1 #include<cstring>
 2 #include<cstdio>
 3 #include<iostream>
 4 #include<queue>
 5 using namespace std;
 6 
 7 #define AA struct ss
 8 
 9 AA{
10 int x,y,t,w;
11 };
12 
13 int n,m,vis[105][105],dir[][5]={{0,1},{0,-1},{1,0},{-1,0}};
14 char g[105][105];
15 int x1,y1,x2,y2,num;
16 
17 int bfs()
18 {
19     AA k;
20     k.x=x1,k.y=y1,k.t=0,k.w=-1;
21     queue<AA>que;
22     que.push(k);
23     while(que.size())
24     {
25         k=que.front();
26         que.pop();
27         if(k.x==x2&&k.y==y2) return 1;
28         for(int i=0;i<4;i++)
29         {
30             AA p=k;
31             p.x+=dir[i][0];
32             p.y+=dir[i][1];
33             p.w=i;
34             if(p.w!=k.w&&k.w!=-1) p.t++;
35             if(p.x>=0&&p.x<n&&p.y>=0&&p.y<m&&p.t<=num)
36             {
37                 if(g[p.x][p.y]=='.'&&(!vis[p.x][p.y]||vis[p.x][p.y]>=p.t))
38                 {
39                     vis[p.x][p.y]=p.t;
40                     que.push(p);
41                 }
42             }
43         }
44     }
45     return -1;
46 }
47 int main()
48 {
49     int T;
50     cin>>T;
51     while(T--)
52     {
53         memset(vis,0,sizeof(vis));
54         cin>>n>>m;
55         for(int i=0;i<n;i++)
56             cin>>g[i];
57         cin>>num>>y1>>x1>>y2>>x2;
58         x1-=1;x2-=1;y1-=1;y2-=1;
59        int ok = bfs();
60         if(ok!=-1) cout<<"yes"<<endl;
61         else cout<<"no"<<endl;
62     }
63 }
View Code

 1983

  1 #include<cstring>
  2 #include<cstdio>
  3 #include<iostream>
  4 #include<queue>
  5 
  6 #define mem(x) memset((x),-1,sizeof(x))
  7 #define AA struct ss
  8 using namespace std;
  9 
 10 int mark,minn,x1,x2,y1,y2,t,n,m,dir[][6]={{0,1},{0,-1},{1,0},{-1,0}},vis[10][10];
 11 char g[10][10];
 12 
 13 AA{
 14     int x,y,step,ko;
 15 };
 16 
 17 bool P(int a,int b)
 18 {
 19     if(a>=0&&a<n&&b>=0&&b<m) return true;
 20     return false;
 21 }
 22 
 23 int solve()
 24 {
 25     int sum1=0,sum2=0;
 26     mem(vis);
 27     for(int i=0;i<4;i++)
 28     {
 29         int x=x1+dir[i][0],y=y1+dir[i][1];
 30         if(P(x,y))
 31         {
 32             if(g[x][y]!='E'&&g[x][y]!='#')
 33             {
 34                 sum1++;
 35             }
 36         }
 37     }
 38     for(int i=0;i<4;i++)
 39     {
 40         int x=x2+dir[i][0],y=y2+dir[i][1];
 41         if(P(x,y))
 42         {
 43             if(g[x][y]!='S'&&g[x][y]!='#')
 44             {
 45                 sum2++;
 46             }
 47         }
 48     }
 49     return sum1<sum2?sum1:sum2;
 50 }
 51 
 52 int bfs()
 53 {
 54     AA k;
 55     k.x=x1,k.y=y1,k.step=0,k.ko=0;
 56     mem(vis);
 57     queue<AA> que;
 58     while(que.size()) que.pop();
 59     que.push(k);
 60 
 61     while(que.size())
 62     {
 63         k=que.front();
 64         que.pop();
 65 
 66         if(k.step>t) continue;
 67         if(g[k.x][k.y]=='J') k.ko=1;
 68         if(g[k.x][k.y]=='E'&&k.ko) return 1;
 69         for(int i=0;i<4;i++)
 70         {
 71             AA p=k;
 72             p.x+=dir[i][0];
 73             p.y+=dir[i][1];
 74             p.step++;
 75             if(P(p.x,p.y))
 76             {
 77                 if(g[p.x][p.y]!='#'&&vis[p.x][p.y]!=p.ko)
 78                 {
 79                     vis[p.x][p.y]=p.ko;
 80                     que.push(p);
 81                 }
 82             }
 83         }
 84     }
 85     return -1;
 86 }
 87 
 88 void dfs(int r,int c,int num,int nn)
 89 {
 90     if(!mark) return;
 91     if(num>=minn) return ;
 92     if(num==nn) {
 93           int  cnt=bfs();
 94         if(cnt==-1){
 95            mark=0;
 96            minn=num;
 97         }
 98             return;
 99             }
100     for(int i=r;i<n&&mark;i++)
101     {
102         for(int j=c;j<m&&mark;j++)
103         {
104             if(g[i][j]=='.'||g[i][j]=='J')
105             {
106                 char c=g[i][j];
107                 g[i][j]='#';
108                 dfs(i,j,num+1,nn);
109                 g[i][j]=c;
110                 if(!mark) return;
111             }
112         }
113     }
114 }
115 
116 int main()
117 {
118     int T;
119     scanf("%d",&T);
120     while(T--)
121     {
122         scanf("%d%d%d",&n,&m,&t);
123         for(int i=0;i<n;i++)
124         {
125             getchar();
126             for(int j=0;j<m;j++)
127             {
128                 scanf("%c",&g[i][j]);
129                 if(g[i][j]=='S') x1=i,y1=j;
130                 if(g[i][j]=='E') x2=i,y2=j;
131             }
132         }
133         minn=solve();
134         int ok=bfs();
135         if(ok!=-1)
136         {
137             mark=1;
138             for(int i=1;i<minn&&mark;i++)
139             dfs(0,0,0,i);
140             printf("%d\n",minn);
141         }
142         else printf("0\n");
143     }
144 }
BUG AC CODE
  1 #include<cstring>
  2 #include<cstdio>
  3 #include<iostream>
  4 #include<queue>
  5 
  6 #define mem(x) memset((x),-1,sizeof(x))
  7 #define AA struct ss
  8 using namespace std;
  9 
 10 int mark,minn,x1,x2,y1,y2,t,n,m,dir[][6]={{0,1},{0,-1},{1,0},{-1,0}},vis[10][10];
 11 char g[10][10];
 12 
 13 AA{
 14     int x,y,step,ko;
 15 };
 16 
 17 bool P(int a,int b)
 18 {
 19     if(a>=0&&a<n&&b>=0&&b<m) return true;
 20     return false;
 21 }
 22 
 23 int solve()
 24 {
 25     int sum=0;
 26     mem(vis);
 27     for(int i=0;i<4;i++)
 28     {
 29         int x=x1+dir[i][0],y=y1+dir[i][1];
 30         if(P(x,y))
 31         {
 32             if(g[x][y]!='E'&&g[x][y]!='#'&&vis[x][y])
 33             {
 34                 sum++;
 35                 vis[x][y]=0;
 36             }
 37         }
 38     }
 39     for(int i=0;i<4;i++)
 40     {
 41         int x=x2+dir[i][0],y=y2+dir[i][1];
 42         if(P(x,y))
 43         {
 44             if(g[x][y]!='S'&&g[x][y]!='#'&&vis[x][y])
 45             {
 46                 sum++;
 47                 vis[x][y]=0;
 48             }
 49         }
 50     }
 51     return sum;
 52 }
 53 
 54 int bfs()
 55 {
 56     AA k;
 57     k.x=x1,k.y=y1,k.step=0,k.ko=0;
 58     mem(vis);
 59     queue<AA> que;
 60     while(que.size()) que.pop();
 61     que.push(k);
 62 
 63     while(que.size())
 64     {
 65         k=que.front();
 66         que.pop();
 67 
 68         if(k.step>t) continue;
 69         if(g[k.x][k.y]=='J') k.ko=1;
 70         if(g[k.x][k.y]=='E'&&k.ko) return 1;
 71         for(int i=0;i<4;i++)
 72         {
 73             AA p=k;
 74             p.x+=dir[i][0];
 75             p.y+=dir[i][1];
 76             p.step++;
 77             if(P(p.x,p.y))
 78             {
 79                 if(g[p.x][p.y]!='#'&&vis[p.x][p.y]!=p.ko)
 80                 {
 81                     vis[p.x][p.y]=p.ko;
 82                     que.push(p);
 83                 }
 84             }
 85         }
 86     }
 87     return -1;
 88 }
 89 
 90 void dfs(int r,int c,int num,int nn)
 91 {
 92     if(!mark) return;
 93     if(num>=minn) return ;
 94     if(num==nn) {
 95           int  cnt=bfs();
 96         if(cnt==-1){
 97            mark=0;
 98            minn=num;
 99         }
100             return;
101             }
102     for(int i=r;i<n&&mark;i++)
103     {
104         for(int j=c;j<m&&mark;j++)
105         {
106             if(g[i][j]=='.'||g[i][j]=='J')
107             {
108                 char c=g[i][j];
109                 g[i][j]='#';
110                 dfs(i,j,num+1,nn);
111                 g[i][j]=c;
112                 if(!mark) return;
113             }
114         }
115     }
116 }
117 
118 int main()
119 {
120     int T;
121     scanf("%d",&T);
122     while(T--)
123     {
124         scanf("%d%d%d",&n,&m,&t);
125         for(int i=0;i<n;i++)
126         {
127             getchar();
128             for(int j=0;j<m;j++)
129             {
130                 scanf("%c",&g[i][j]);
131                 if(g[i][j]=='S') x1=i,y1=j;
132                 if(g[i][j]=='E') x2=i,y2=j;
133             }
134         }
135         minn=solve();
136         int ok=bfs();
137         if(ok!=-1)
138         {
139             mark=1;
140             for(int i=1;i<minn&&mark;i++)
141             dfs(0,0,0,i);
142             printf("%d\n",minn);
143         }
144         else printf("0\n");
145     }
146 }
TLE BUT RIGHT CODE

 2102

 1 #include<cstring>
 2 #include<cstdio>
 3 #include<iostream>
 4 #include<queue>
 5 using namespace std;
 6 #define AA struct ss
 7 AA
 8 {
 9   int x,y,ceil,step;
10 };
11 
12 char g[2][11][11];
13 int x1,y1,n,m,t,dir[][5]={{0,1},{0,-1},{1,0},{-1,0}};
14 
15 bool bfs()
16 {
17     queue<AA>que;
18     while(que.size()) que.pop();
19     AA k;
20     k.x=0,k.y=0,k.ceil=0,k.step=0;
21     g[k.ceil][k.x][k.y]='*';
22     que.push(k);
23     while(que.size())
24     {
25         k=que.front();
26         que.pop();
27       //  cout<<k.ceil<<' '<<k.x<<' '<<k.y<<endl;
28         for(int i=0;i<4;i++)
29         {
30             AA p=k;
31             p.x+=dir[i][0];
32             p.y+=dir[i][1];
33             p.step++;
34             if(p.step>t) break;
35             if(p.x>=0&&p.x<n&&p.y>=0&&p.y<m)
36             {
37                 if(g[p.ceil][p.x][p.y]!='*')
38                 {
39                     if(g[p.ceil][p.x][p.y]=='#')
40                     {
41                         //que.push(p);
42                         if(p.ceil==0) p.ceil=1;
43                         else p.ceil=0;
44                     }
45                     if(g[p.ceil][p.x][p.y]=='*') continue;
46                     if(g[p.ceil][p.x][p.y]=='#') continue;
47                     if(g[p.ceil][p.x][p.y]=='P') return true;
48                     g[p.ceil][p.x][p.y]='*';
49                     que.push(p);
50                 }
51             }
52         }
53     }
54     return false;
55 }
56 
57 int main()
58 {
59     int T;
60     cin>>T;
61     while(T--)
62     {
63         cin>>n>>m>>t;
64         for(int i=0;i<n;i++)
65         cin>>g[0][i];
66         for(int i=0;i<n;i++)
67         cin>>g[1][i];
68 
69         if(bfs()) cout<<"YES"<<endl;
70         else cout<<"NO"<<endl;
71     }
72 }
View Code

 2553

 1 #include<cstring>
 2 #include<cstdio>
 3 #include<iostream>
 4 
 5 using namespace std;
 6 
 7 int n,vis[5][20],cnt;
 8 
 9 void dfs(int cur)
10 {
11     if(cur==n) {cnt++;return;}
12     for(int i=0;i<n;i++)
13     {
14         if(!vis[0][i]&&!vis[1][cur+i]&&!vis[2][cur-i+n])
15         {
16              vis[0][i]=vis[1][cur+i]=vis[2][cur-i+n]=1;
17              dfs(cur+1);
18              vis[0][i]=vis[1][cur+i]=vis[2][cur-i+n]=0;
19         }
20     }
21 }
22 
23 int main()
24 {
25    // int a[]={1,0,0,2,10,4,40,92,352,724};
26     while(scanf("%d",&n)!=EOF&&n)
27     {
28         cnt=0;
29         memset(vis,0,sizeof(vis));
30         dfs(0);
31         printf("%d\n",cnt);
32     }
33 }
View Code

 2563

 1 #include<cstring>
 2 #include<cstdio>
 3 #include<iostream>
 4 
 5 using namespace std;
 6 
 7 int g[50][50],dir[][5]={{0,-1},{0,1},{1,0}};
 8 int n,cnt;
 9 
10 void dfs(int cur,int r,int c)
11 {
12     if(cur==n) {cnt++;return;}
13     for(int i=0;i<3;i++)
14     {
15         int x=r+dir[i][0],y=c+dir[i][1];
16         if(!g[x][y])
17         {
18             g[x][y]=1;
19             dfs(cur+1,x,y);
20             g[x][y]=0;
21         }
22     }
23 }
24 
25 int main()
26 {
27     int a[]={3,7,17,41,99,239,577,1393,3363,8119,19601,47321,114243,275807,665857,1607521,3880899,9369319,22619537,54608393};
28     int T;
29     cin>>T;
30     while(T--)
31     {
32         cin>>n;
33         cnt=0;
34         memset(g,0,sizeof(g));
35         g[25][25]=1;
36         dfs(0,25,25);
37         cout<<"    "<<cnt<<endl;
38     }
39 }
View Code

 2612

 1 #include<cstring>
 2 #include<cstdio>
 3 #include<iostream>
 4 #include<queue>
 5 using namespace std;
 6 
 7 #define AA struct ss
 8 AA
 9 {
10     int x,y,t;
11 };
12 int n,m,x1,y1,x2,y2,vis[200][200],dir[][3]={{0,1},{0,-1},{1,0},{-1,0}},step1[200][200],step2[200][200];
13 char g[200][200];
14 
15 void bfs(int a,int b,int num)
16 {
17     queue<AA>que;
18     while(que.size()) que.pop();
19     AA k;
20     k.x=a;k.y=b;k.t=0;
21     que.push(k);
22     vis[k.x][k.y]=1;
23     while(que.size())
24     {
25         k=que.front();
26         que.pop();
27        // cout<<k.x<<' '<<k.y<<' '<<k.t<<' '<<g[k.x][k.y]<<endl;
28         for(int i=0;i<4;i++)
29         {
30             AA p=k;
31             p.x+=dir[i][0];
32             p.y+=dir[i][1];
33             p.t++;
34             if(p.x>=0&&p.x<n&&p.y>=0&&p.y<m)
35             {
36                 if(g[p.x][p.y]!='#'&&!vis[p.x][p.y])
37                 {
38                     if(g[p.x][p.y]=='@')
39                     {
40                          if(num==0&&!step1[p.x][p.y])   step1[p.x][p.y]=p.t;
41                         else if(!step2[p.x][p.y]&&num==1) step2[p.x][p.y]=p.t;
42                     }
43                     vis[p.x][p.y]=1;
44                     que.push(p);
45                 }
46             }
47         }
48     }
49 }
50 int main()
51 {
52     while(cin>>n>>m)
53     {
54         for(int i=0;i<n;i++)
55             for(int j=0;j<m;j++)
56         {
57             cin>>g[i][j];
58             if(g[i][j]=='Y') x1=i,y1=j;
59             if(g[i][j]=='M') x2=i,y2=j;
60         }
61         memset(step1,0,sizeof(step1));
62         memset(step2,0,sizeof(step2));
63         memset(vis,0,sizeof(vis));
64         bfs(x1,y1,0);
65         memset(vis,0,sizeof(vis));
66         bfs(x2,y2,1);
67         int cnt=0x3f3f3f3f;
68         for(int i=0;i<n;i++)
69             for(int j=0;j<m;j++)
70         {
71             if(g[i][j]=='@')
72             {
73                 if(step1[i][j]&&step2[i][j])
74                 cnt=min(cnt,step1[i][j]+step2[i][j]);
75             }
76         }
77         cout<<cnt*11<<endl;
78     }
79 }
View Code 直接从Y到M 标号不回头 会MLE 两次bfs 减少储存数据 同时注意必须@地方的步数

 2614

 1 #include<cstring>
 2 #include<cstdio>
 3 #include<iostream>
 4 using namespace std;
 5 
 6 int n,g[20][20],cnt,vis[20];
 7 
 8 void dfs(int r,int num,int t)
 9 {
10     if(num>cnt) cnt=num;
11     for(int j=0;j<n;j++)
12     {
13         if(g[r][j]>=t&&!vis[j])
14         {
15             vis[j]=1;
16             dfs(j,num+1,g[r][j]);
17             vis[j]=0;
18         }
19     }
20 
21 }
22 
23 int main()
24 {
25     while(cin>>n)
26     {
27         cnt=0;
28         for(int i=0;i<n;i++)
29             for(int j=0;j<n;j++)
30             cin>>g[i][j];
31       //  for(int i=0;i<n;i++)
32        // {
33             memset(vis,0,sizeof(vis));
34             vis[0]=1;
35             dfs(0,1,0);
36         //}
37         cout<<cnt<<endl;
38     }
39 }
View Code

 2717

 1 #include<cstring>
 2 #include<cstdio>
 3 #include<iostream>
 4 #include<queue>
 5 #define AA struct ss
 6 using namespace std;
 7 
 8 int n,k,vis[100006];
 9 
10 AA
11 {
12     int s,t;
13 };
14 int bfs()
15 {
16     memset(vis,0,sizeof(vis));
17     queue<AA> que;
18     AA m;
19     m.s=n;m.t=0;
20     que.push(m);
21     vis[m.s]=1;
22 
23     while(que.size())
24     {
25         m=que.front();
26         que.pop();
27         if(m.s==k) return m.t;
28 
29         AA p=m;
30         p.s+=1;
31         p.t++;
32         if(p.s>=0&&p.s<=100000&&!vis[p.s])
33         {
34             vis[p.s]=1;
35             que.push(p);
36         }
37         p=m;
38         p.s-=1;
39         p.t++;
40         if(p.s>=0&&p.s<=100000&&!vis[p.s])
41         {
42             vis[p.s]=1;
43             que.push(p);
44         }
45         p=m;
46         p.s*=2;
47         p.t++;
48         if(p.s>=0&&p.s<=100000&&!vis[p.s])
49         {
50             vis[p.s]=1;
51             que.push(p);
52         }
53     }
54 }
55 int main()
56 {
57     while(cin>>n>>k)
58     {
59        int cnt= bfs();
60         cout<<cnt<<endl;
61     }
62 }
View Code

 

posted on 2015-02-24 12:34  wsa  阅读(89)  评论(0编辑  收藏  举报

导航