WA了,求指点
查看原帖
WA了,求指点
1288642
lmn985楼主2025/1/13 19:31
#include <bits/stdc++.h>
#define st first
#define nd second
#define ll long long
#define int long long
#define MP make_pair
#define rep(i,a,b) for(int i=a;i<b;i++)
#define REP(i,a,b) for(int i=a;i>=b;i--)
#define RE cout<<"Ha Ha , you have an RE"
using namespace std;
ll quickpow(ll a,ll k,ll mod){
	ll res=1;
	while(k){
		if(k&1)res=res*a;
		a=a*a;
		k>>=1;
	}
	return res;
}
int read(){
    int x=0,w=1;
    char ch=0;
    while(ch<'0' or ch>'9'){
        if(ch=='-')w=-1;
        ch=getchar();
    }
    while(ch>='0' and ch<='9')x=x*10+ch-48,ch=getchar();
    return x*w;
}
ll llread(){
    ll x=0;
	int w=1;
    char ch=0;
    while(ch<'0' or ch>'9'){
        if(ch=='-')w=-1;
        ch=getchar();
    }
    while(ch>='0' and ch<='9')x=x*10+(ll)(ch-48),ch=getchar();
    return x*(ll)w;
}
#define print(a,n) for(int i=0;i<n;i++){write(a[i]);putchar(' ');}
int gcd(int x,int y){
    return (x==0 or y==0)?x+y:gcd(min(x,y),max(x,y)%min(x,y));
}
int lcm(int a,int b){
    return a/gcd(a,b)*b;
}
int string_to_int(string s){
    int cur=0;
    for(int i=0;i<s.size();i++)
        cur*=10,cur+=s[i]-'0';
     return cur;
}
string int_to_string(int x){
    string cur;
    while(x){
        cur+=x%10+'0';
        x/=10;
    }
    reverse(cur.begin(),cur.end());
    return cur;
}
const int N=2e3+5,dx[4]={0,0,-1,1},INF=1000000005;
const ll LINF=1000000000000000005;
//省空间,能不开longlong的绝不开longlong
//AC不是终点,AC了也要不停优化
int n,m,d[N][N];
bool used[N][N];
char a[N][N];
void bfs(int s,int t){
	queue<pair<int,pair<int,int> > > w;
	used[s][t]=1;
	w.push(MP(s,MP(t,-1)));
	while(w.size()){
		int x=w.front().st,y=w.front().nd.st,lr=w.front().nd.nd;
		w.pop();
		rep(r,0,4){
			if(lr==1 and r<2)continue;
			if(lr==0 and r>=2)continue;
			int lx=x+dx[r],ly=y+dx[3-r];
			if(lx>=0 and ly>=0 and lx<n and ly<m)
			    if(a[lx][ly]!='#' and !used[lx][ly]){
			    	used[lx][ly]=1;
					w.push(MP(lx,MP(ly,r<2)));
					d[lx][ly]=d[x][y]+1;
			}
		}
	}
}
void solve(){
	cin>>n>>m;
	rep(i,0,n)rep(j,0,m)
		cin>>a[i][j];
	rep(i,0,n)rep(j,0,m)if(a[i][j]=='S')bfs(i,j);
	rep(i,0,n)rep(j,0,m)if(a[i][j]=='G'){
		if(!used[i][j])d[i][j]--;
		cout<<d[i][j];
	}
}
signed main(){
	int T=1;
	//cin>>T;
	while(T--)solve();
    return 0;
}
2025/1/13 19:31
加载中...