POJ 2312 Battle City Priority multi-column +BFS

Source: Internet
Author: User

Source: http://poj.org/problem?id=2312

Test instructions: The title background is the battle of the tanks when you were young, and how many steps you need at least from the beginning to the end. It is known that s and R can not go, E is empty, can go, B is brick, only after the knock can pass.

Idea: It is easy to see that this is a wide-search topic, but because of the time to go e and go B is not the same, so you can not use the normal queue to save points. Because to go to B, we have to get rid of bricks before they pass, so we can understand that to go B takes two steps, and walk e refers to the need for 1 steps. Therefore, you cannot use a normal queue to save. We can use the priority queue in the STL to save, each time from the team head is a few steps of the point on it. So the first search is the optimal solution.

Code:

#include <iostream> #include <cstdio> #include <queue> #include <string.h> #include <

Algorithm> #include <string> using namespace std;
	#define CLR (Arr,val) memset (arr,val,sizeof (arr)) struct point{int x,y,step;
	BOOL operator < (const point & P) const{return step > P.step;
} 
};
const int N = 305;
Char Map[n][n];
int sx,sy,ex,ey,flag[n][n],row,col;
int addx[4] = {0,0,1,-1};
int addy[4] = {1,-1,0,0};
	int BFs () {priority_queue<point> qq;
	Point P; p.x = SX; P.Y = sy;
	P.step = 0;
	Flag[sx][sy] = 1;
	Qq.push (P);
		while (!qq.empty ()) {Point Topp = Qq.top ();
		Qq.pop ();
		if (topp.x = = Ex && topp.y = = ey) {return topp.step;
			   } else{for (int i = 0; i < 4; ++i) {int newx = topp.x + addx[i];
			   int newy = topp.y + addy[i];
				   if (newx >= 0 && newx < row && newy >= 0 && newy < col &&!flag[newx][newy]) { if (map[newx][newy] = = ' S ' | | map[newx][newy] = = 'R ') continue;
				  Point Newp;
				  newp.x = newx;
				  NEWP.Y = Newy;
				  Flag[newx][newy] = 1;
				  if (map[newx][newy] = = ' B ') {newp.step = Topp.step + 2;
				  } else Newp.step = Topp.step + 1;
			   Qq.push (NEWP);
}}}} return-1;
	} int main () {//freopen ("1.txt", "R", stdin);
	   while (scanf ("%d%d", &row,&col)! = EOF) {if (row + col = = 0) break;
	   CLR (flag,0);
	   CLR (map, ' 0 ');
			  for (int i = 0; i < row, ++i) {for (int j = 0; J < col; ++j) {cin >> map[i][j];
				if (map[i][j] = = ' Y ') {sx = i;
			  sy = j;
				} else if (map[i][j] = = ' T ') {ex = i;
			  EY = j;
	  }}} int ans = BFS ();
	printf ("%d\n", ans);
} 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.