[kuangbin带你飞]专题一 简单搜索


A – 棋盘问题  POJ – 1321 




每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n <= 8 , k <= n 

当为-1 -1时表示输入结束。 

随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, . 表示空白区域(数据保证不出现多余的空白行或者空白列)。 


对于每一组数据,给出一行输出,输出摆放的方案数目C (数据保证C<2^31)。

Sample Input

2 1
4 4
-1 -1

Sample Output


#include <iostream> #include <stdio.h> #include <string> #include <string.h> #include <algorithm> #include <math.h>
using namespace std; typedef long long LL; const int maxn=15; int n,k; char s[maxn][maxn]; bool vis[maxn]; int ans; void dfs(int x,int cnt) { if(cnt>=k) { ans++; return; } for(int i=x;i<n;i++) { for(int j=0;j<=n;j++) { if(s[i][j]=='#'&&vis[j]==false) { vis[j]=true; dfs(i+1,cnt+1); vis[j]=false;//每次选择了,后面要记得消除标记 } } } } int main() { while(scanf("%d %d",&n,&k)!=EOF) { if(n==-1&&k==-1) break; memset(vis,false,sizeof(vis)); ans=0; for(int i=0;i<n;i++) scanf("%s",s[i]); dfs(0,0); printf("%d\n",ans); } return 0; }


B – Dungeon Master POJ – 2251 

You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides. 

Is an escape possible? If yes, how long will it take? 


The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size). 

L is the number of levels making up the dungeon. 

R and C are the number of rows and columns making up the plan of each level. 

Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a ‘#’ and empty cells are represented by a ‘.’. Your starting position is indicated by ‘S’ and the exit by the letter ‘E’. There’s a single blank line after each level. Input is terminated by three zeroes for L, R and C.


Each maze generates one line of output. If it is possible to reach the exit, print a line of the form 

Escaped in x minute(s).

where x is replaced by the shortest time it takes to escape. 

If it is not possible to escape, print the line 


Sample Input

3 4 5



1 3 3

0 0 0

Sample Output

Escaped in 11 minute(s).

#include <iostream> #include <stdio.h> #include <string> #include <string.h> #include <algorithm> #include <math.h> #include <queue>
using namespace std; typedef long long LL; const int maxn=35; int l,r,c; int ans; bool flag; int sx,sy,sz; char s[maxn][maxn][maxn]; bool vis[maxn][maxn][maxn]; int dx[]={1,-1,0,0,0,0}; int dy[]={0,0,1,-1,0,0}; int dz[]={0,0,0,0,1,-1}; struct Node { int x,y,z; int step; }; Node now,temp,nextt; void bfs() { int x,y,z; queue<Node>q; vis[sz][sx][sy]=1; now.x=sx; now.y=sy; now.z=sz; now.step=0; q.push(now); while(!q.empty()) { temp=q.front(); q.pop(); for(int i=0;i<6;i++) { x=nextt.x=temp.x+dx[i]; y=nextt.y=temp.y+dy[i]; z=nextt.z=temp.z+dz[i]; nextt.step=temp.step+1; if(x<0||x>=r||y<0||y>=c||z<0||z>=l)//判断边界 continue; if(s[z][x][y]=='#') continue; if(s[z][x][y]=='E')//到终点跳出。 { ans=nextt.step; flag=true; return; } if(s[z][x][y]=='.'&&vis[z][x][y]==false) { vis[z][x][y]=true; q.push(nextt); } } } } int main() { int i,j,k; while(scanf("%d %d %d",&l,&r,&c)!=EOF) { if(l==0&&r==0&&c==0) break; memset(vis,false,sizeof(vis)); flag=false; for(i=0;i<l;i++) { for(j=0;j<r;j++) { scanf("%s",s[i][j]); for(k=0;k<c;k++) { if(s[i][j][k]=='S') { sx=j;sy=k;sz=i; } } } } bfs(); if(flag) printf("Escaped in %d minute(s).\n",ans); else printf("Trapped!\n"); } return 0; }

C – Catch That Cow POJ – 3278 

Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.

* Walking: FJ can move from any point X to the points – 1 or + 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.

If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?


Line 1: Two space-separated integers: N and K


Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.

Sample Input

5 17

Sample Output


题解:给定两个整数n和k通过 n+1或n-1 或n*2 这3种操作,使得n==k,输出最少的操作次数
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <queue>
using namespace std; const int maxn=100005; struct Node { int pos; int step; }; queue<Node>q; bool vis[maxn]; int n,k; void bfs() { Node now,temp,nextt; now.pos=n;now.step=0; q.push(now); vis[now.pos]=true; while(!q.empty()) { temp=q.front(); q.pop(); if(temp.pos==k) { printf("%d\n",temp.step); return; } else { nextt.pos=temp.pos-1; if(nextt.pos>=0&&vis[nextt.pos]==false)//向后退,但是注意一定要大于0 { vis[nextt.pos]=true; nextt.step=temp.step+1; q.push(nextt); } nextt.pos=temp.pos+1; if(nextt.pos<=maxn&&vis[nextt.pos]==false)//向前进,但注意要小于maxn { vis[nextt.pos]=true; nextt.step=temp.step+1; q.push(nextt); } nextt.pos=temp.pos*2; if(nextt.pos<=maxn&&vis[nextt.pos]==false)//向前跳,但是要小于maxn { vis[nextt.pos]=true; nextt.step=temp.step+1; q.push(nextt); } } } } int main() { memset(vis,false,sizeof(vis)); scanf("%d %d",&n,&k); if(n>=k)//如果在牛的后面,那么一直向后退就是最优的走法。 printf("%d\n",n-k); else bfs(); return 0; }


E – Find The Multiple  POJ – 1426 

Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no more than 100 decimal digits.


The input file may contain multiple test cases. Each line contains a value of n (1 <= n <= 200). A line containing a zero terminates the input.


For each value of n in the input print a line containing the corresponding value of m. The decimal representation of m must not contain more than 100 digits. If there are multiple solutions for a given value of n, any one of them is acceptable.

Sample Input


Sample Output


题意:一个数,只有01组成,找到能够整除n的值。那么对于每个位枚举0,1一直跑,每次看能否整除。最后得到的结果就行了。注意要开long long
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <queue>
using namespace std; typedef long long LL; const int maxn=100005; int n; void bfs() { queue<LL>q;//注意开long long q.push(1);//不可能以0开头 LL temp; while(!q.empty()) { temp=q.front(); q.pop(); if(temp%n==0) { printf("%lld\n",temp); return; } q.push(temp*10); q.push(temp*10+1); } } int main() { while(scanf("%d",&n)!=EOF) { if(n==0) break; bfs(); } return 0; }


F – Prime Path  POJ – 3126 

The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices. 

— It is a matter of security to change such things every now and then, to keep the enemy in the dark. 

— But look, I have chosen my number 1033 for good reasons. I am the Prime minister, you know! 

— I know, so therefore your new number 8179 is also a prime. You will just have to paste four new digits over the four old ones on your office door. 

— No, it’s not that simple. Suppose that I change the first digit to an 8, then the number will read 8033 which is not a prime! 

— I see, being the prime minister you cannot stand having a non-prime number on your door even for a few seconds. 

— Correct! So I must invent a scheme for going from 1033 to 8179 by a path of prime numbers where only one digit is changed from one prime to the next prime. 

Now, the minister of finance, who had been eavesdropping, intervened. 

— No unnecessary expenditure, please! I happen to know that the price of a digit is one pound. 

— Hmm, in that case I need a computer program to minimize the cost. You don’t know some very cheap software gurus, do you? 

— In fact, I do. You see, there is this programming contest going on… Help the prime minister to find the cheapest prime path between any two given four-digit primes! The first digit must be nonzero, of course. Here is a solution in the case above. 








The cost of this solution is 6 pounds. Note that the digit 1 which got pasted over in step 2 can not be reused in the last step – a new 1 must be purchased.


One line with a positive number: the number of test cases (at most 100). Then for each test case, one line with two numbers separated by a blank. Both numbers are four-digit primes (without leading zeros).


One line for each case, either with a number stating the minimal cost or containing the word Impossible.

Sample Input

1033 8179
1373 8017
1033 1033

Sample Output


题意:给定两个素数a b,求a变幻到b需要几步
#include <stdio.h> #include <string.h> #include <iostream> #include <queue> #include <math.h>

using namespace std; const int maxn=100005; int T,n,m; bool vis[maxn]; struct Node { int x; int step; }; bool judge_prime(int n)//判断是否为素数,这是之前写的代码,其实建议先用线性筛筛出素数打表,复杂度会低一些。 { if(n==1) return 0; for(int i=2;i<=sqrt(n);i++) { if(n%i==0) return false; } return true; } void bfs() { memset(vis,0,sizeof(vis)); Node now,temp,nextt; now.x=n; now.step=0; vis[now.x]=1; queue<Node>q; q.push(now); while(!q.empty()) { temp=q.front(); q.pop(); if(temp.x==m) { printf("%d\n",temp.step); return ; } for(int d=0;d<4;d++) { if(d==0)//个位
 { for(int i=1;i<=9;i+=2)//个位上偶数就不可能是素数,所以是+2 { nextt=temp; nextt.x=(temp.x)/10*10+i; if(nextt.x!=temp.x&&!vis[nextt.x]&&judge_prime(nextt.x)) { vis[nextt.x]=1; nextt.step=temp.step+1; q.push(nextt); } } } else if(d==1)//十位
 { for(int i=0;i<=9;i++) { nextt=temp; nextt.x=temp.x/100*100+i*10+temp.x%10; if(nextt.x!=temp.x &&!vis[nextt.x]&&judge_prime(nextt.x)) { vis[nextt.x]=1; nextt.step=temp.step+1; q.push(nextt); } } } else if(d==2)//百位
 { for(int i=0;i<=9;i++) { nextt=temp; nextt.x=temp.x/1000*1000+i*100+temp.x%100; if(nextt.x!=temp.x &&!vis[nextt.x]&&judge_prime(nextt.x)) { vis[nextt.x]=1; nextt.step=temp.step+1; q.push(nextt); } } } else if(d==3) { for(int i=1;i<=9;i++) { nextt.x=i*1000+temp.x%1000; if(nextt.x!=temp.x&&!vis[nextt.x]&&judge_prime(nextt.x)) { vis[nextt.x]=1; nextt.step=temp.step+1; q.push(nextt); } } } } } printf("Impossible\n"); return ; } int main() { scanf("%d",&T); while(T--) { scanf("%d %d",&n,&m); bfs(); } return 0; }


G – Shuffle’m Up POJ – 3087 

A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of poker chips, S1 and S2, each stack containing C chips. Each stack may contain chips of several different colors.

The actual shuffle operation is performed by interleaving a chip from S1 with a chip from S2 as shown below for C = 5:

The single resultant stack, S12, contains 2 * C chips. The bottommost chip of S12 is the bottommost chip from S2. On top of that chip, is the bottommost chip from S1. The interleaving process continues taking the 2nd chip from the bottom of S2 and placing that on S12, followed by the 2nd chip from the bottom of S1 and so on until the topmost chip from S1 is placed on top of S12.

After the shuffle operation, S12 is split into 2 new stacks by taking the bottommost C chips from S12 to form a new S1 and the topmost C chips from S12 to form a new S2. The shuffle operation may then be repeated to form a new S12.

For this problem, you will write a program to determine if a particular resultant stack S12 can be formed by shuffling two stacks some number of times.


The first line of input contains a single integer N, (1 ≤ N ≤ 1000) which is the number of datasets that follow.

Each dataset consists of four lines of input. The first line of a dataset specifies an integer C, (1 ≤ C ≤ 100) which is the number of chips in each initial stack (S1and S2). The second line of each dataset specifies the colors of each of the C chips in stack S1, starting with the bottommost chip. The third line of each dataset specifies the colors of each of the C chips in stack S2 starting with the bottommost chip. Colors are expressed as a single uppercase letter (A through H). There are no blanks or separators between the chip colors. The fourth line of each dataset contains 2 * C uppercase letters (A through H), representing the colors of the desired result of the shuffling of S1 and S2 zero or more times. The bottommost chip’s color is specified first.


Output for each dataset consists of a single line that displays the dataset number (1 though N), a space, and an integer value which is the minimum number of shuffle operations required to get the desired resultant stack. If the desired result can not be reached using the input for the dataset, display the value negative 1 (−1) for the number of shuffle operations.

Sample Input


Sample Output

1 2
2 -1


#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <queue> #include <stack> #include <map>
using namespace std; typedef long long LL; const int maxn=205; const int INF=0x3f3f3f3f; int T,n,m; char s1[maxn],s2[maxn]; char s[maxn*2]; map<string,bool>vis; int main() { scanf("%d",&T); int casee=1; while(T--) { scanf("%d",&n); scanf("%s",s1);//输入两个字符串 scanf("%s",s2); scanf("%s",s); vis[s]=true; int step=0; while(1) { char temp[maxn*2]; int cnt=0; for(int i=0;i<n;i++)//两个字符串合并 { temp[cnt++]=s2[i]; temp[cnt++]=s1[i]; } temp[cnt]='\0'; step++; if(!strcmp(s,temp))//看是否和给定的结果一样。 { printf("%d %d\n",casee++,step); break; } else if(vis[temp]&&strcmp(s,temp))//看是否出现了循环,如果出现了循环并且没有和给定的结果一样,说明就不会再出现了。 { printf("%d -1\n",casee++); break; } vis[temp]=true; int i,j; for(i=0;i<n;i++)//上述的都不符合的话就把这个重新拆成两个字符串 s1[i]=temp[i]; s1[i]='\0';//记得最后还有这个\0 for(j=0;i<2*n;i++,j++) s2[j]=temp[i]; s2[i]='\0'; } } return 0; }


J – Fire!  UVA – 11624 



#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <queue> #include <stack>
using namespace std; typedef long long LL; const int maxn=1005; const int INF=0x3f3f3f3f; int T,n,m,ans; char s[maxn][maxn]; bool vis[maxn][maxn]; int timee[maxn][maxn]; int dx[]={0,0,1,-1}; int dy[]={1,-1,0,0}; struct Node { int x,y; int step; }; queue<Node>q; void prebfs()//给file跑bfs,预处理到达每个空闲地的最短时间记录到timee数组里面 { Node temp,nextt; while(!q.empty()) { temp=q.front(); q.pop(); for(int i=0;i<4;i++) { nextt.x=temp.x+dx[i]; nextt.y=temp.y+dy[i]; if(vis[nextt.x][nextt.y]==false&&s[nextt.x][nextt.y]=='.') { vis[nextt.x][nextt.y]=true; nextt.step=temp.step+1; timee[nextt.x][nextt.y]=nextt.step; q.push(nextt); } } } } bool bfs(int ii,int jj) { // for(int i=1;i<=n;i++) // { // for(int j=1;j<=m;j++) // printf("%d ",timee[i][j]); // printf("\n"); // }
    memset(vis,false,sizeof(vis)); queue<Node>qq; Node now,temp,nextt; now.x=ii;now.y=jj;now.step=0; vis[ii][jj]=true; qq.push(now); while(!qq.empty()) { temp=qq.front(); qq.pop(); for(int i=0;i<4;i++) { nextt.x=temp.x+dx[i]; nextt.y=temp.y+dy[i]; if(nextt.x<1||nextt.y<1||nextt.x>n||nextt.y>m) { ans=temp.step+1; return true; } if(vis[nextt.x][nextt.y]==false&&s[nextt.x][nextt.y]=='.'&&temp.step+1<timee[nextt.x][nextt.y]) { vis[nextt.x][nextt.y]=true; nextt.step=temp.step+1; qq.push(nextt); } } } return false; } int main() { scanf("%d",&T); while(T--) { while(!q.empty()) q.pop(); memset(timee,INF,sizeof(timee)); memset(vis,false,sizeof(vis)); memset(s,'#',sizeof(s)); scanf("%d %d",&n,&m); for(int i=1;i<=n;i++) scanf("%s",s[i]+1); for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(s[i][j]=='F')//多个fire点 { Node now; now.x=i;now.y=j;timee[i][j]=0;now.step=0; vis[i][j]=true; q.push(now); } } } prebfs(); for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(s[i][j]=='J') { if(bfs(i,j)) printf("%d\n",ans); else printf("IMPOSSIBLE\n"); } } } } return 0; }


K – 迷宫问题 POJ – 3984 


int maze[5][5] = {
0, 1, 0, 0, 0,
0, 1, 0, 1, 0,
0, 0, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 1, 0,



一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。



Sample Input

0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0

Sample Output

(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)

#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <queue> #include <stack>
using namespace std; typedef long long LL; const int maxn=10; int ma[maxn][maxn]; bool vis[maxn][maxn]; int fa[30]; int dx[]={0,0,1,-1}; int dy[]={1,-1,0,0}; struct Node { int x,y; }; bool check(Node a)//判断边界 { if(a.x>0&&a.y>0&&a.x<=5&&a.y<=5&&vis[a.x][a.y]==false) return true; else
        return false; } void show() { stack<Node>s; int t=25; Node temp; temp.x=5;temp.y=5; s.push(temp); while(fa[t]!=-1) { int a=fa[t]; // cout<<a<<endl;
        temp.x=a/5+1;temp.y=a%5; if(temp.y==0) { temp.y=5; temp.x--; } s.push(temp); t=a; } while(!s.empty()) { temp=s.top(); printf("(%d, %d)\n",temp.x-1,temp.y-1); s.pop(); } } void bfs() { queue<Node>q; Node now,temp,nextt; now.x=1;now.y=1; vis[1][1]=true; q.push(now); while(!q.empty()) { temp=q.front(); q.pop(); if(temp.x==5&&temp.y==5) { show(); return; } else { for(int i=0;i<4;i++) { nextt.x=temp.x+dx[i]; nextt.y=temp.y+dy[i]; if(check(nextt)&&ma[nextt.x][nextt.y]==0) { vis[nextt.x][nextt.y]=true; fa[(nextt.x-1)*5+nextt.y]=(temp.x-1)*5+temp.y; q.push(nextt); } } } } } int main() { memset(fa,-1,sizeof(fa)); for(int i=1;i<=5;i++) for(int j=1;j<=5;j++) scanf("%d",&ma[i][j]); bfs(); return 0; }


L – Oil Deposits HDU – 1241 

The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid. 

InputThe input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*’, representing the absence of oil, or `@’, representing an oil pocket. 
OutputFor each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets. 
Sample Input

1 1
3 5
1 8
5 5 
0 0 

Sample Output


#include <iostream> #include <cstdio> #include <string> #include <string.h> #include <cmath> #include <algorithm> #include <queue>
using namespace std; const int maxn=105; int m,n,ans; char s[maxn][maxn]; bool vis[maxn][maxn]; int dx[8]={0,1,1,1,0,-1,-1,-1}; int dy[8]={-1,-1,0,1,1,1,0,-1}; struct Node { int x,y; }; void bfs(int ii,int jj) { queue<Node>q; Node now,temp,nextt; now.x=ii;now.y=jj; vis[ii][jj]=true; q.push(now); while(!q.empty()) { temp=q.front(); q.pop(); for(int i=0;i<8;i++) { nextt.x=temp.x+dx[i]; nextt.y=temp.y+dy[i]; if(s[nextt.x][nextt.y]=='@'&&vis[nextt.x][nextt.y]==false) { vis[nextt.x][nextt.y]=true; q.push(nextt); } } } } int main() { while(scanf("%d %d",&m,&n)!=EOF) { if(m==0&&n==0) break; memset(vis,false,sizeof(vis)); memset(s,'*',sizeof(s)); ans=0; for(int i=1;i<=m;i++) scanf("%s",s[i]+1); //printf("0000000\n");
        for(int i=1;i<=m;i++) { for(int j=1;j<=n;j++) { if(s[i][j]=='@'&&vis[i][j]==false) { bfs(i,j); ans++; } } } printf("%d\n",ans); } return 0; }


N – Find a way HDU – 2612 

Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki. 

Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest. 

Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes. 

InputThe input contains multiple test cases. 
Each test case include, first two integers n, m. (2<=n,m<=200). 
Next n lines, each line included m character. 
‘Y’ express yifenfei initial position. 
‘M’    express Merceki initial position. 
‘#’ forbid road; 
‘.’ Road. 
‘@’ KCF 
OutputFor each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet.Sample Input

4 4
4 4
5 5

Sample Output


#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <queue>
using namespace std; const int maxn=205; const int INF=0x3f3f3f3f; int n,m; char s[maxn][maxn]; bool vis[maxn][maxn]; int dx[]={0,0,1,-1}; int dy[]={1,-1,0,0}; struct Node { int x,y; int step; }; int bfs(int ii,int jj) { int ansy,ansm; memset(vis,false,sizeof(vis)); queue<Node>q; Node now,temp,nextt; now.x=ii;now.y=jj;now.step=0; bool flagy=false;bool flagm=false; q.push(now); while(!q.empty()) { temp=q.front(); q.pop(); if(flagm==true&&flagy==true)//判断是否两个点都到达了 return ansm+ansy; for(int i=0;i<4;i++) { nextt.x=temp.x+dx[i]; nextt.y=temp.y+dy[i]; if(s[nextt.x][nextt.y]!='#'&&vis[nextt.x][nextt.y]==false) { vis[nextt.x][nextt.y]=true; if(s[nextt.x][nextt.y]=='Y') { flagy=true; ansy=temp.step+1;//到达y需要的步数 } else if(s[nextt.x][nextt.y]=='M') { flagm=true; ansm=temp.step+1;//达到m需要的步数 } else { nextt.step=temp.step+1; q.push(nextt); } } } } return INF; } int main() { while(scanf("%d %d",&n,&m)!=EOF) { memset(s,'#',sizeof(s)); for(int i=1;i<=n;i++) scanf("%s",s[i]+1); int ans=INF; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(s[i][j]=='@')//对于每个@都跑一遍bfs,记录最小值 { ans=min(ans,bfs(i,j)); } } } printf("%d\n",ans*11); } return 0; }