码迷,mamicode.com
首页 > 其他好文 > 详细

uva 563 Crimewave 最短路径

时间:2015-05-17 12:19:08      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:bfs

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <cstring>

#define INF 100000000
using namespace std;
int a,b,s;


struct node{
	int x,y;
	bool operator != (const node& a)const{
		return !(x==a.x&& y==a.y);
	}
};
node q[2505];
node pre[55][55];
int vis[55][55];
int va[][2] = {{0,1},{1,0},{-1,0},{0,-1}};
int ma[55][55];

int bfs(node a){
	queue<node> que;
	memset(pre,0,sizeof(pre));
	pre[a.x][a.y] = a;
	que.push(a);
	memset(vis,0,sizeof(vis));
	
	vis[a.x][a.y] = 1;
	
	int flag = 1;
	node an;	
	while(!que.empty() && flag == 1){
		node c = que.front();
		que.pop();
		for(int i = 0;i < 4;i++){
			node v;
			v.x = c.x + va[i][0];
			v.y = c.y + va[i][1];
			if(ma[v.x][v.y] == 2){
				flag = 0;
				pre[v.x][v.y] = c;
				an = c; 
				break;
			}
			else if(ma[v.x][v.y] == 1){
				if(vis[v.x][v.y] != 1){
					vis[v.x][v.y] = 1;
					pre[v.x][v.y] = c;
					que.push(v);
				}
			}
		}		
	}
	if(flag){
		return 0;
	}
	else{
		while(pre[an.x][an.y] != an){
			ma[an.x][an.y] = 0;
			an = pre[an.x][an.y];
		}
		return 1;
	}
}
int main(){
	int  t;
	cin >> t;
	while(t--){
		cin >> a >> b >> s;
		for(int i = 0;i <= a+1;i++){
			for(int j = 0;j <= b+1;j++){
				ma[i][j] = 2;
			}
		}
		for(int i = 1;i <= a;i++){
			for(int j = 1;j <= b;j++){
				ma[i][j] = 1;
			}
		}
		
		for(int i = 0;i < s;i++){
			scanf("%d%d",&q[i].x,&q[i].y);
			ma[q[i].x][q[i].y] = 0;
		}
		
		int flag = 1;
		
		for(int i = 0;i < s;i++){
			if(!bfs(q[i]))
			{
				
				
				flag = 0;
				break;
			}
			
		}
		
		if(flag){
			cout << "possible" << endl;
		}
		else{
			cout << "not possible" << endl;
		}
	
	}
	return 0;
}

uva 563 Crimewave 最短路径

标签:bfs

原文地址:http://blog.csdn.net/qq_24667639/article/details/45787335

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!