• 【搜索】棋盘 luogu-3956


    分析

    按照这个题目随便写一个搜索就可以了

    AC代码

    #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
    ");
        else printf("%d
    ",ans);
        return 0;
    }
    
  • 相关阅读:
    递归
    HDU_oj_2041 超级楼梯
    树与森林——树与森林的遍历
    HUD_oj_2040 亲和数
    HDU_oj_2039 判定三角形
    HDU_oj_2037 今年暑假不AC
    多边形面积
    HDU_oj_2036 改革春风吹满地(多边形面积)
    【转发】【composer】composer 命令行介绍
    【chm】【windows】win7下chm打开不显示内容
  • 原文地址:https://www.cnblogs.com/Dawn-Star/p/9707580.html
Copyright © 2020-2023  润新知