标签:lin print i++ int har amp printf c代码 code
按照这个题目随便写一个搜索就可以了
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <ctype.h>
#include <iostream>
using namespace std;
const int dx[4]={-1,0,1,0};
const int dy[4]={0,-1,0,1};
const int inf=1<<30;
int a[105][105],color[105][105],f[105][105];
int m,n,ans;
inline int read() {
int x=0,w=0;char ch=0;
while (!isdigit(ch)) {w|=ch=='-';ch=getchar();}
while (isdigit(ch)) {x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
return w?-x:x;
}
void dfs(int x,int y,bool flag,int sum) {
if (sum>=f[x][y]) return ;
f[x][y]=sum;
if (x==m && y==m) {
ans=min(ans,sum);
return ;
}
for (int i=0;i<4;i++) {
int nx=x+dx[i],ny=y+dy[i];
if (nx<1||nx>m||ny<1||ny>m) continue;
if (color[nx][ny]==-1) {
if (flag) {
color[nx][ny]=color[x][y];
dfs(nx,ny,0,sum+2);
color[nx][ny]=-1;
}
}
else {
if (color[nx][ny]==color[x][y]) dfs(nx,ny,1,sum);
else dfs(nx,ny,1,sum+1);
}
}
}
int main() {
m=read(),n=read();
for (int i=1;i<=m;i++) for (int j=1;j<=m;j++) color[i][j]=-1,f[i][j]=inf;
for (int i=1;i<=n;i++) {
int x=read(),y=read(),c=read();
color[x][y]=c;
}
ans=inf;
dfs(1,1,1,0);
if (ans==inf) printf("-1\n");
else printf("%d\n",ans);
return 0;
}
标签:lin print i++ int har amp printf c代码 code
原文地址:https://www.cnblogs.com/Dawn-Star/p/9707580.html