View Code of Problem 3499

#include<iostream>
#include<stack>
#include<queue>
#include<Cstring>
using namespace std;
struct node{
	stack<int>x;
	stack<int>y;
};
int x[4]={0,-1,0,1};
int y[4]={-1,0,1,0};
int a[5][5];
bool f[5][5];
void bfs(){
	struct node l;
	l.x.push(0);
	l.y.push(0);
	queue<node>q;
	q.push(l);
	while(!q.empty()){
		for(int i=0;i<4;++i){
			int xx=q.front().x.top()+x[i];
			int yy=q.front().y.top()+y[i];
			if(xx>=0&&xx<5&&yy>=0&&yy<5&&a[xx][yy]!=1&&f[xx][yy]==false){
				f[xx][yy]=true;
				struct node k;
				k.x=q.front().x;
				k.y=q.front().y;
				k.x.push(xx);
				k.y.push(yy);
				q.push(k);
				if(xx==4&&yy==4){
					stack<int>x_r,y_r,x,y;
					x=k.x;
					y=k.y;
					while(!x.empty()){
						x_r.push(x.top());
						x.pop();
						y_r.push(y.top());
						y.pop();
					}
					while(!x_r.empty()){
						printf("(%d, %d)\n",x_r.top(),y_r.top());
						x_r.pop();
						y_r.pop();
					}
				}
			}
		}
		q.pop();
	}	
}
int main(){
	memset(f,false,sizeof(f));
	for(int i=0;i<5;++i)
		for(int j=0;j<5;++j)
			scanf("%d",&a[i][j]);
	bfs();
}

Double click to view unformatted code.


Back to problem 3499