M - Find a way ~~~ [kuangbin帶你飛]專題一 簡單搜尋
阿新 • • 發佈:2018-12-09
hsj和lsh最近迷上了pokemon go的遊戲。在雙十一大物期中考試來臨之前,他們想抓一隻稀有土撥鼠來攢攢人品(因為土撥鼠的重新整理地點最近來到了哈工程)
但是由於土撥鼠過於強大,他的雷霆半月斬以及驚天浪濤沙都可以輕鬆的將他們兩擊敗,但是他們兩的合擊必殺技流影電光閃以及天羽屠鼠舞可以將土撥鼠打至昏迷狀態,並可將其捕獲。
但是因為這是款按時間付費的遊戲,他們需要儘快捕捉到土撥鼠(即他們兩到土撥鼠的時間之和需要最少),因此他們找到了你來幫他們解決這個問題。 規定每走一步需要花費11分鐘。
Input
輸入存在多組(需使用!=EOF)
每組的第一行有兩個整數n,m(2<=n,m<=200)
接下來n行,每行包括m個字元
‘Y’表示hsj所在的位置
‘M’表示lsh所在的位置
‘.’表示可以通過的地方
‘#’表示教學樓即不能走的地方
‘@’表示稀有土撥鼠重新整理的地方(地圖中存在多隻稀有土撥鼠)
Output
對於每組樣例輸出他們到達土撥鼠重新整理點的最小時間總和。
保證每組樣例都存在一個土撥鼠重新整理點,他們兩都能到達
Sample Input
4 4 Y.#@ .... .#.. @..M 4 4 Y.#@ .... .#.. @#.M 5 5 [email protected] .#... .#... @..M. #...#
Sample Output
66 88 66
思路 : 遍歷全圖尋找能走到的點需要的時間;
逐個找的話會超時;
下面是TLE程式碼
#include<iostream> #include<stdio.h> #include<queue> #include<string.h> #include<algorithm> #include<string> #include<stack> #define ll long long using namespace std; char a[205][205]; int book[205][205]; struct Node { int x,y,s; Node() {} Node(int xx,int yy,int ss) : x(xx) , y(yy) ,s(ss){} }; int n,m; int yx,yy,mx,my,ex,ey; const int dx[] = {0,0,1,-1}; const int dy[] = {1,-1,0,0}; int bfs(int x,int y,int s) { memset(book,0,sizeof(book)); queue<Node> Q; Q.push(Node(x,y,s)); book[x][y] = 1; while(!Q.empty()) { Node u = Q.front() ; Q.pop() ; if(u.x == ex && u.y == ey) { return u.s; } for(int i=0 ; i<4 ; i++) { int xx = u.x + dx[i] ; int yy = u.y + dy[i] ; int ss = u.s + 1 ; if(xx>=0 && yy>=0 && xx<n && yy<m && a[xx][yy] != '#' && book[xx][yy] == 0) { book[xx][yy] = 1 ; Q.push(Node(xx,yy,ss)) ; } } } return -1 ; } int main() { while(cin>>n>>m) { for(int i=0 ; i<n ; i++) { for(int j=0 ; j<m ; j++) { cin>>a[i][j] ; if(a[i][j] == 'Y') { yx = i ; yy = j ; } if(a[i][j] == 'M') { mx = i ; my = j ; } } } int ans1,ans2; int ans = 99999 ; for(int i=0 ; i<n ; i++) { for(int j=0 ; j<m ; j++) { if(a[i][j] == '@') { ex = i ; ey = j ; } ans1 = bfs(yx,yy,0) ; ans2 = bfs(mx,my,0) ; int data = ans1 + ans2 ; ans = min(ans,data) ; } } cout<<ans*11<<endl; } return 0; }
經過zhy 和 zcr 學長的思路提供 我寫出來了不TLE的程式碼
#include<iostream> #include<stdio.h> #include<queue> #include<string.h> #include<algorithm> #include<string> #include<stack> #define ll long long using namespace std; char a[205][205]; int book[205][205]; int book1[205][205]; struct Node { int x,y,s; Node() {} Node(int xx,int yy,int ss) : x(xx) , y(yy) ,s(ss){} }; int n,m; int yx,yy,mx,my,ex,ey; const int dx[] = {0,0,1,-1}; const int dy[] = {1,-1,0,0}; int bfs(int x,int y,int s) { memset(book,0,sizeof(book)); queue<Node> Q; Q.push(Node(x,y,s)); book[x][y] = -1; while(!Q.empty()) { Node u = Q.front() ; Q.pop() ; for(int i=0 ; i<4 ; i++) { int xx = u.x + dx[i] ; int yy = u.y + dy[i] ; int ss = u.s + 1 ; if(xx>=0 && yy>=0 && xx<n && yy<m && a[xx][yy] != '#' && book[xx][yy] == 0) { book[xx][yy] = ss ; Q.push(Node(xx,yy,ss)) ; } } } return -1 ; } int bfs1(int x,int y,int s) { memset(book1,0,sizeof(book1)); queue<Node> Q; Q.push(Node(x,y,s)); book1[x][y] = -1; while(!Q.empty()) { Node u = Q.front() ; Q.pop() ; for(int i=0 ; i<4 ; i++) { int xx = u.x + dx[i] ; int yy = u.y + dy[i] ; int ss = u.s + 1 ; if(xx>=0 && yy>=0 && xx<n && yy<m && a[xx][yy] != '#' && book1[xx][yy] == 0) { book1[xx][yy] = ss ; Q.push(Node(xx,yy,ss)) ; } } } return -1 ; } int main() { while(cin>>n>>m) { for(int i=0 ; i<n ; i++) { for(int j=0 ; j<m ; j++) { cin>>a[i][j] ; if(a[i][j] == 'Y') { yx = i ; yy = j ; } if(a[i][j] == 'M') { mx = i ; my = j ; } } } bfs(yx,yy,0); bfs1(mx,my,0); int ans1,ans2; int ans = 99999 ; for(int i=0 ; i<n ; i++) { for(int j=0 ; j<m ; j++) { if(a[i][j] == '@' && book[i][j] != 0 && book1[i][j] != 0) { int data = book[i][j] + book1[i][j] ; ans = min(ans,data) ; } } } cout<<ans*11<<endl; /* for(int i=0 ; i<n ; i++) { for(int j=0 ; j<m ; j++) { cout<<book[i][j]<<" "; } cout<<endl; } cout<<endl; for(int i=0 ; i<n ; i++) { for(int j=0 ; j<m ; j++) { cout<<book1[i][j]<<" "; } cout<<endl; } */ } return 0; }