sw
  • 板块灌水区
  • 楼主dsbs
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/10/3 10:31
  • 上次更新2024/10/3 13:04:30
查看原帖
sw
1048824
dsbs楼主2024/10/3 10:31
#include<bits/stdc++.h>
using namespace std;
char s[30][30];
int way[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
int n,m;
struct str{
	int n,m,x;
};
bool check(int x,int y,int u){
	if(x>n||x<1||y>m||y<1||(s[x][y]>='0'&&s[x][y]<='9'?u-s[x][y]-'0'<=0:u<=0)||s[x][y]=='#'){
		return false;
	}
	return true;
}
int main(){
	//freopen("spring.in","r",stdin);
	//freopen("spring.out","w",stdout);
	int u;
	cin>>u;
	for(int i=1;i<=u;i++){
		cin>>n>>m;
		memset(s,0,sizeof(s));
		//memset(w,0,sizeof(w));
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				cin>>s[n][m];
			}
		}
		queue<str> q;
		str st;
		st.n=n;
		st.m=m;
		st.x=10;
		q.push(st);
		bool flag=true;
		while(!q.empty()){
			for(int i=0;i<4;i++){
				if(check(q.front().n+way[1][i],q.front().m+way[0][i],q.front().x)){
					st.n=q.front().n+way[1][i];
					st.m=q.front().m+way[0][i];
					if(st.n==n&&st.m==m){
						cout<<"possible"<<'\n';
						flag=false;
					}
					st.x=(s[st.n][st.m]>='0'&&s[st.n][st.m]<='9'?q.front().x-s[st.n][st.m]-'0':q.front().x);
					q.push(st);
				}
			}
			if(!flag){
				break;
			}
		}
		if(flag){
			cout<<"impossible"<<'\n';
		}
	}
	return 0;
}
2024/10/3 10:31
加载中...