我跟你差不多,你多少分
```cpp
#include<bits/stdc++.h>
using namespace std;
const int N=120;
int a[N][N];
int n,m;
bool vis[N][N];
int dx[4]={0,1,-1,0};
int dy[4]={1,0,0,-1};
int ans=0x3f3f3f3f;
int dp[N][N][2];
bool check(int x,int y)
{
return x<=n&&y<=n&&x>0&&y>0&&(!vis[x][y]);
}
int dfs(int x,int y,int color)
{
if(dp[x][y][color]!=-1)return dp[x][y][color];
if(x==n&&y==n)
{
return 0;
}
int ret=0x3f3f3f3f;
//printf("%d %d %d %d\n",x,y,now,color);
for(int i=0;i<=3;i++)
{
int xx=dx[i]+x;
int yy=dy[i]+y;
if(check(xx,yy))
{
vis[xx][yy]=1;
if(a[xx][yy]==color)
{
ret=min(ret,dfs(xx,yy,a[xx][yy]));
}
else if(a[xx][yy]==-1&&a[x][y]!=-1)
{
if(color==1)ret=min(ret,dfs(xx,yy,1)+2);
else ret=min(ret,dfs(xx,yy,1)+3);
if(color==0)ret=min(ret,dfs(xx,yy,0)+2);
else ret=min(ret,dfs(xx,yy,0)+3);
}
else if(a[xx][yy]!=color&&a[xx][yy]!=-1)
{
ret=min(ret,dfs(xx,yy,a[xx][yy])+1);
}
vis[xx][yy]=0;
}
}
dp[x][y][color]=ret;
return ret;
}
int main()
{
memset(dp,-1,sizeof(dp));
memset(a,-1,sizeof(a));
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
a[x][y]=z;
}
vis[1][1]=1;
ans=dfs(1,1,a[1][1]);
if(ans==0x3f3f3f3f)printf("-1");
else printf("%d",ans);
return 0;
}
```
by Wanna_Accepted @ 2021-01-31 20:59:01
我也没t
40分
by Wanna_Accepted @ 2021-01-31 21:01:50
这是一个蓝名且不会debug的蒟蒻
by Digital_Sunrise @ 2021-02-02 14:42:03