#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 最短路径
最新推荐文章于 2020-05-14 21:03:02 发布