uva 563 Crimewave 最短路径

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值