Hangzhou Electric -1026ignatius and the Princess I (bfs+ record Path)

Source: Internet
Author: User
Tags bool printf time limit

Ignatius and the Princess ITime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)Total submission (s): 15010 Accepted Submission (s): 4766 special Judge
Problem Description The princess have been abducted by the Beelzebub feng5166, our hero Ignatius have to rescue our pretty P Rincess. Now he gets into feng5166 ' s castle. The castle is a large labyrinth. To make the problem simply, we assume the labyrinth are a n*m two-dimensional array which left-top corner is (0,0) and Righ T-bottom Corner is (n-1,m-1). Ignatius enters at (0,0), and the door to Feng5166 's are at (n-1,m-1), that's our target. There is some monsters in the castle, if Ignatius meet them, he had to kill them. Here is some rules:

1.Ignatius can only moves in four directions, one step per second. A step is defined as Follow:if current position is (x, y), after a step, Ignatius can only stand on (x-1,y), (X+1,y), (x, Y -1) or (x,y+1).
2.The array is marked with some characters and numbers. We define them like this:
. : The place where Ignatius can walk on.
X:the Place was a trap, Ignatius should not walk on it.
N:here is a monster with n HP (1<=n<=9), if Ignatius walk on it, it takes him n seconds to kill the monster.

Your task is to give out the path which costs minimum seconds for Ignatius to reach target position. Assume that the start position and the target position would never be a trap, and there would never be a monster at The start position.

Input the input contains several test cases. Each test case starts with a line contains the numbers N and M (2<=n<=100,2<=m<=100) which indicate the size of The Labyrinth. Then a n*m two-dimensional array follows, which describe the whole labyrinth. The input is terminated by the end of file. More details in the Sample Input.

Output for each test case, you should output "God's help to our poor hero." If Ignatius can ' t reach the target position, Or you should output ' It takes n seconds to reach the target position and let me show you the ' the '. (n is the minimum seconds), and the hero the whole path. Output a line contains the "FINISH" after all test case. If there is more than one path, any one was OK in this problem. More details in the Sample Output.

Sample Input

5 6. Xx.1 ... X.2.2...X .... XX. XXXXX. 5 6. Xx.1 ... X.2.2...X .... XX. XXXXX1 5 6. Xx..... XX1. 2...X .... XX. XXXXX.
Sample Output
It takes seconds to reach of the target position, let me show you the the The. 1s: (0,0), (1,0) 2s: (1,0), (+) 3s: (), (2,1) 4s: (2,1), (2,2) 5s: (2,2), (2,3) 6s: (2,3), (1,3) 7s :(1,3) (1,4) 8s:fight at (1,4) 9s:fight at (1,4) 10s: (1,4), (1,5) 11s: (1,5), (2,5) 12s: (2,5), (3,5) 13s: ( 3,5) (4,5) FINISH It takes seconds to reach the target position, let me show you the The. 1s: (0,0), (1,0) 2s: (1,0), (+) 3s: (), (2,1) 4s: (2,1), (2,2) 5s: (2,2), (2,3) 6s: (2,3), (1,3) 7s :(1,3) (1,4) 8s:fight at (1,4) 9s:fight at (1,4) 10s: (1,4), (1,5) 11s: (1,5), (2,5) 12s: (2,5), (3,5) 13s: ( 3,5) (4,5) 14s:fight at (4,5) FINISH God's help to our poor hero. FINISH


This question looks very difficult, actually also is not simple.

The main meaning is, from the upper left corner to the lower right corner, if you can go to, record the path and output.

The path I'm using here is the struct array + stack.

Be careful to empty the queue before using the queue, otherwise the Hyper memory

#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <stack
> Using namespace std; 
int m,n,vis[102][102],mov[4][2]={0,1,0,-1,1,0,-1,0};
Char map[102][102];
	struct Node {friend bool operator< (node A,node b) {return a.step>b.step;
} int x,y,step;
}TA,TB;
	struct Nodex {int x;
int y;
}mm;
Nodex best[102][102];
priority_queue<node>q;
Stack<nodex> W; BOOL Can (node X) {if (x.x<0| | x.x>m-1| | x.y<0| | x.y>n-1| | vis[x.x][x.y]| |
	map[x.x][x.y]== ' X ') return false;
return true;
	} int BFs () {int i,cot=0;
	ta.x=0;
	ta.y=0;
	Ta.step=0;
	while (!q.empty ()) Q.pop ();//otherwise hyper memory vis[ta.x][ta.y] = 1;
	Q.push (TA);
		while (!q.empty ()) {ta=q.top ();
		Q.pop ();
		if (ta.x==m-1&&ta.y==n-1) return ta.step;
			for (i=0;i<4;i++) {tb.x=ta.x+mov[i][0];
			TB.Y=TA.Y+MOV[I][1];
				if (Can (TB)) {mm.x=ta.x;
				MM.Y=TA.Y; if (map[tb.x][tb.y]>= ' 1 ' &&map[tb.x][tb.y]<= ' 9 ') tb.step=ta.step+map[tb.x][tb.y]-' 0 ' +1;
				
				else tb.step=ta.step+1;
				Vis[tb.x][tb.y]=1;
				Q.push (TB);
			best[tb.x][tb.y]=mm;
}}} return 0;
	} int main () {int i;
		while (scanf ("%d%d", &m,&n)!=eof) {for (i=0;i<m;i++) scanf ("%s", Map[i]);
		memset (vis,0,sizeof (VIS));
		int T=bfs ();
		if (!t) printf ("God's help to our poor hero.\nfinish\n");
			else {printf ("It takes%d seconds to reach the target position, let me show you the way.\n", T);
			Nodex QQ;
			Best[0][0].x=-1; 
			Best[0][0].y=-1;
			Qq.x=m-1;
			qq.y=n-1;
				while (best[qq.x][qq.y].x!=-1&&best[qq.x][qq.y].y!=-1) {W.push (QQ);
			QQ=BEST[QQ.X][QQ.Y];
			} int time=1;
				while (!w.empty ()) {printf ("%ds: (%d,%d)-(%d,%d) \ n", Time++,qq.x,qq.y,w.top (). X,w.top (). y);
				Qq=w.top ();
				if (map[qq.x][qq.y]!= '. ')
				{for (i=1;i<map[qq.x][qq.y]-' 0 ' +1;i++) printf ("%ds:fight at (%d,%d) \ n", time++,qq.x,qq.y);
			} w.pop ();
		} printf ("finish\n"); }} return 0; }



Here's what I wrote with Dfs, but it's timed out.


#include <cstdio> #include <cstring> #include <algorithm> #define INF 0x3f3f3f3f using namespace std;
Char map[110][110]; 
int M,n,nex[10100],vis[110][110],mov[4][2]={0,1,0,-1,1,0,-1,0},best,cot;
int bes[10100]; BOOL Can (int x,int y) {if (x<0| | x>m-1| | y<0| | y>n-1| | map[x][y]== ' x ' | |
	Vis[x][y]) return false;
return true;
	} void Dfs (int x,int y,int time) {int i,j;
			if (x==m-1&&y==n-1) {if (time<best) {best=time;
		for (i=0;i<cot;i++) bes[i]=nex[i];
	} return;
		} for (i=0;i<4;i++) {int xx=x+mov[i][0],yy=y+mov[i][1];
			if (Can (Xx,yy)) {vis[xx][yy]=1;
				if (map[xx][yy]>= ' 1 ' &&map[xx][yy]<= ' 9 ') {for (j=0;j<=map[xx][yy]-' 0 '; j + +) Nex[cot++]=xx*n+yy;
				DFS (xx,yy,time+map[xx][yy]-' 0 ' + 1);
			cot-= (map[xx][yy]-' 0 ' + 1);
				} else {nex[cot]=xx*n+yy;
				cot++;
				DFS (XX,YY,TIME+1);
			cot--;
		} vis[xx][yy]=0;
	}}} int main () {int i,j;
		while (scanf ("%d%d", &m,&n)!=eof) {for (i=0;i<m;i++) scanf ("%s", Map[i]);
		Best=inf;
		memset (vis,0,sizeof (VIS));
		memset (nex,0,sizeof (NEX));
		memset (Bes,0,sizeof (BES));
		Vis[0][0]=1;
		nex[0]=0;
		Cot=1;
		DFS (0,0,0);
		if (best==inf) printf ("God's help to our poor hero.\nfinish\n");	
			else {printf ("It takes%d seconds to reach the target position, let me show you the way.\n", best);
				for (i=1;i<=best;i++) {int x1=bes[i-1]/n;
				int y1=bes[i-1]%n;
				int x2=bes[i]/n;
				int y2=bes[i]%n;
				if (Bes[i]!=bes[i-1]) {printf ("%ds: (%d,%d) (%d,%d) \ n", i,x1,y1,x2,y2);
				} else {printf ("%ds:fight at (%d,%d) \ n", i,x2,y2);		
		}} printf ("finish\n");
}} 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.