Nyist 284 & amp; poj 2312 tank war (deformation BFS)

Source: Internet
Author: User

This is a prototype of the tank war. It is a variant of the maze. Given a map of mxn, there are ordinary bricks B, bricks S, and river R on the map, open space E, and a treasure location T, and your location Y, find the minimum number of steps to eat the treasure (two steps are required for tanks to pass through ordinary brick B, open space E step, cannot pass through the bricks and rivers )..

 

#include <queue>#include <iostream>using namespace std;int n,m;int mintime[305][305];char map[305][305];struct point{int x,y,time;}s,e;int h[4][2]={1,0,-1,0,0,-1,0,1};queue<point> q;int bfs(point s){int i,j,x,y; point t,tt;while(!q.empty()) q.pop();q.push(s); mintime[s.x][s.y]=0;while(!q.empty()){t=q.front(); q.pop();for(i=0;i<4;i++){x=t.x+h[i][0]; y=t.y+h[i][1];if(x>=0&&x<n&&y>=0&&y<m&&map[x][y]!='R'&&map[x][y]!='S'){tt.x=x; tt.y=y; tt.time=t.time+1;if(map[x][y]=='B') tt.time++;if(tt.time<mintime[x][y]){q.push(tt); mintime[x][y]=tt.time;}}}}return mintime[e.x][e.y];}int main(int argc, char *argv[]){int i,j;while(cin>>n>>m&&(n+m)){for(i=0;i<n;i++)for(j=0;j<m;j++){cin>>map[i][j]; mintime[i][j]=100000005;if(map[i][j]=='Y') { s.x=i;  s.y=j; s.time=0;}if(map[i][j]=='T') { e.x=i;  e.y=j;}}int ans=bfs(s);if(ans<100000005) cout<<ans<<endl;else cout<<-1<<endl;}return 0;}

 

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.