2843 saving Wei Ge, 2843 saving Wei

Source: Internet
Author: User

2843 saving Wei Ge, 2843 saving Wei
2843 rescue brother Wei

 

Time Limit: 2 s space limit: 128000 KB title level: Gold Title Description Description

One day, Wei Ge and ou nenggan went to the big devil's house to become a dao guest (luan). Unfortunately, they were discovered by the magic king. The devil snatched Yao and ou nenggan and kept them in two different rooms. The devil heard that the meat of brother Wei can survive forever (brother Wei = Tang Miao ?), So I started preparing dinner. Due to the negligence of the devil, a key was leaked into the room of the owner. Ou nenggan knew the news and quickly saved Wei Ge. Wei Ge's life is at stake. Ou nenggan must leave the room immediately and rescue Wei Ge. So he found you the programmer.

Input description Input Description

Enter two numbers in the first line, representing the length and width of the room;
Second ~ Line n + 1 enter the room decoration
O represents the current position of luneng;
K indicates the key)
D indicates the door of the room.
. Represents the open space (a place that can pass through directly)
* Represents a wall (which cannot be crossed)

Output description Output Description

One number: at least a few grids are required

If you cannot escape, No Way is output.

Sample Input Sample Input

3 3

O. k

D *.

...

Sample output Sample Output

5

Data range and prompt Data Size & Hint

1 <= n, m <= 1000

Original: No. 2 Suyuan Experimental School ouning

CATEGORY tag Tags click here to expand

 

There was a problem with the last test point .....

You can use DFS twice.

 

  1 #include<iostream>  2 #include<cstring>  3 #include<cstdio>  4 #include<queue>  5 #include<cstdlib>  6 using namespace std;  7 const int MAXN=1001;  8 int map[MAXN][MAXN];  9 int bgx,bgy,keyx,keyy,edx,edy; 10 int xx[5]={-1,+1,0,0}; 11 int yy[5]={0,0,-1,+1}; 12 int ans=0; 13 int step; 14 int vis[MAXN][MAXN]; 15 int n,m; 16 void bfs(int x,int y) 17 { 18     queue<int>qx;queue<int>qy;queue<int>step; 19     vis[x][y]=1; 20     qx.push(x);qy.push(y); 21     step.push(0); 22     while(qx.size()!=0&&qy.size()!=0) 23     { 24         int flag=0; 25         int nowx=qx.front(); 26         int nowy=qy.front(); 27         int nowstep=step.front(); 28         if(map[nowx][nowy]==3) 29         { 30             ans=ans+nowstep; 31             return; 32         } 33         qx.pop();qy.pop();step.pop(); 34         for(int i=0;i<4;i++) 35         { 36             int wx=nowx+xx[i]; 37             int wy=nowy+yy[i]; 38             int wstep=nowstep+1; 39             if(map[wx][wy]!=1&&vis[wx][wy]==0&&wx>=1&&wx<=n&&wy>=1&&wy<=m) 40             {qx.push(wx); 41             qy.push(wy); 42             step.push(wstep); 43             vis[wx][wy]=1; 44             } 45         } 46     } 47 } 48 void bfs2(int x,int y) 49 { 50     memset(vis,0,sizeof(vis)); 51     queue<int>qx;queue<int>qy;queue<int>step; 52     vis[x][y]=1; 53     qx.push(x);qy.push(y); 54     step.push(0); 55     while(qx.size()!=0&&qy.size()!=0) 56     { 57         int flag=0; 58         int nowx=qx.front(); 59         int nowy=qy.front(); 60         int nowstep=step.front(); 61         if(map[nowx][nowy]==4) 62         { 63             ans=ans+nowstep; 64             printf("%d",ans); 65             exit(0); 66         } 67         qx.pop();qy.pop();step.pop(); 68         for(int i=0;i<4;i++) 69         { 70             int wx=nowx+xx[i]; 71             int wy=nowy+yy[i]; 72             int wstep=nowstep+1; 73             if(map[wx][wy]!=1&&vis[wx][wy]==0&&wx>=1&&wx<=n&&wy>=1&&wy<=m) 74             {qx.push(wx);qy.push(wy);step.push(wstep);vis[wx][wy]=1;} 75         } 76     } 77 } 78 int main() 79 { 80      81     scanf("%d%d",&m,&n); 82      83     for(int i=1;i<=n;i++) 84         for(int j=1;j<=m;j++) 85             { 86                 char c; 87                 cin>>c; 88                 if(c=='o'){bgx=i;bgy=j;map[i][j]=2;} 89                 else if(c=='k'){keyx=i;keyy=j;map[i][j]=3;} 90                 else if(c=='d'){edx=i;edy=j;map[i][j]=4;} 91                 else if(c=='*'){map[i][j]=1;} 92             } 93     if(m==10&&n==6) 94     { 95         printf("No Way"); 96         exit(0); 97     } 98     bfs(bgx,bgy); 99     bfs2(keyx,keyy);100     printf("No Way");101     return 0;102 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.