N只有50,跑一下N的平方建图,两种颜色之间的距离就是 他前面的Y的数量,剩下的就是模板了。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<set>
#include<queue>
using namespace std;
typedef long long ll;
typedef pair<int,int > P;
const int maxn = 55;
const int inf = 1<<20;
int n;
char bmap[maxn][maxn] ;
int edge[maxn][maxn];
int dist[maxn];
struct node
{
int id;
int dist;
bool operator < (const node& T) const{
return dist>T.dist;
}
node(int a,int b):id(a),dist(b){}
};
void dij()
{
for(int i=0;i<n;i++) dist[i] = inf;
priority_queue<node>q;
q.push(node(0,0)); dist[0] = 0;
while(!q.empty())
{
node fr = q.top(); q.pop();
int u = fr.id;
//printf("%d\n",u);
for(int i=0;i<n;i++)
{
if(dist[i]>dist[u]+edge[u][i])
{
//printf("sdfsdfsdfsdfsd\n");
dist[i] = dist[u] + edge[u][i];
q.push(node(i,dist[i]));
}
}
}
if(dist[n-1]>=inf) printf("-1\n");
else printf("%d\n",dist[n-1]);
}
int main()
{
int cases;
scanf("%d",&cases);
while(cases--)
{
scanf("%d",&n);
for(int i=0;i<n;i++) scanf("%s",bmap[i]);
for(int i=0;i<n;i++)
{
int sum = 0;
for(int j=0;j<n;j++)
{
if(bmap[i][j]=='N') {edge[i][j] = inf; continue;}
edge[i][j] = sum++;
}
}
dij();
}
return 0;
}