要从终点跑一遍bfs才不会t
#include<bits/stdc++.h>
using namespace std;
const int N=1001;
int a[N][N];
int n,m,q;
int dist[100010];
int res[N][N];
int dx[4]={0,1,0,-1};
int dy[4]={-1,0,1,0};
int bfs(int x,int y){
bool st[101][101];
memset(st,false,sizeof st);
//st[i][j]=false;
queue<pair<pair<int,int>,int>>q;
q.push({{x,y},0});
st[x][y]=true;
while(q.size()){
auto t=q.front();
q.pop();
int x=t.first.first,y=t.first.second;
//cout<<x<<" "<<y<<endl;
if(a[x][y]==1){
res[x][y]=t.second;
//cout<<a[x][y]<<" "<<t.second<<endl;
}
for(int i=0;i<4;i++){
int xx=x+dx[i];
int yy=y+dy[i];
if(xx>n||yy>m||xx<1||yy<1||st[xx][yy]||a[xx][yy]==0)continue;
q.push({{xx,yy},t.second+1});
st[xx][yy]=true;
}
}
}
int main(){
cin.tie(nullptr);cout.tie(nullptr);ios::sync_with_stdio(false);
cin>>n>>m;
int x0,y0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
res[i][j]=1e9+7;
cin>>a[i][j];
if(a[i][j]==2)x0=i,y0=j;
}
}
bfs(x0,y0);
cin>>q;
for(int i=1;i<=q;i++)dist[i]=1e9+7;
map<int,vector<int>>ma;
for(int i=1;i<=q;i++){
int x,y;
cin>>x>>y;
// cout<<x<<" "<<y<<endl;
dist[i]=res[y][x];
//cout<<dist[i]<<endl;
}
for(int i=1;i<=q;i++){
if(dist[i]!=1e9+7)
ma[dist[i]].push_back(i);
}
int ff=1;
for(auto &tt:ma){
//for(int i=0;i<tt.second.size();i++)cout<<tt.second[i]<< " ";
//cout<<endl;
if(tt.second.size()==1){
ff=0;
cout<<tt.second[0]<<" "<<tt.first;
break;
}
}
if(ff!=0)cout<<"No winner.";
return 0;
}