zoukankan      html  css  js  c++  java
  • 【搜索】棋盘

    原题传送门

    思路


    这是一道经典深搜题,难度适中,属于那种我刚开始没思路,但思考一会总能做出来的那种最适合我的难度,很不错。
    这道题是NOIP2017普及组T3,比起NOIP2018普及组的毒瘤T3不知道好了多少倍QAQ。

    Code


    #include<iostream>
    #include<cstdio>
    #include<string>
    #include<vector>
    #include<algorithm>
    #include<cstdlib>
    #include<cmath>
    #include<stack>
    #include<map>
    using namespace std;
    
    int M,N,ans=99999999,x,y,c,m[105][105],f[105][105]; 
    const int nextx[5]={0,-1,1,0,0};
    const int nexty[5]={0,0,0,-1,1};
    
    void dfs(int x,int y,int cost,int flag)
    {
    	if(x<1||y<1||x>M||y>M)return;
    	if(cost>=f[x][y])return;
    	f[x][y]=cost;
    	if(x==M&&y==M)
    	{
    		ans=min(ans,cost);
    		return;
    	}
    	for(int k=1;k<=4;k++)
    	{
    		int tx=x+nextx[k];
    		int ty=y+nexty[k];
    		//若同色
    		if(m[tx][ty]==m[x][y]&&m[tx][ty]!=-1)
    		{
    			dfs(tx,ty,cost,0);
    		}
    		//若不同色
    		if(m[tx][ty]!=m[x][y]&&m[tx][ty]!=-1)
    		{
    			dfs(tx,ty,cost+1,0);
    		}
    		//若无色
    		if(m[tx][ty]==-1)
    		{
    			if(flag==0)
    			{
    				m[tx][ty]=m[x][y];
    				dfs(tx,ty,cost+2,1);
    				m[tx][ty]=-1;
    			}
    		}
    	}
    } 
    
    int main()
    {
        cin>>M>>N;
    	for(int i=1;i<=M;i++)
    	{
    		for(int j=1;j<=M;j++)
    		{
    			m[i][j]=-1;
    			f[i][j]=99999999;
    		}
    	}
        for(int i=1;i<=N;i++)
        {
        	cin>>x>>y>>c;
        	m[x][y]=c;
    	}
    	dfs(1,1,0,0);
    	if(ans==99999999)
    		cout<<-1;
    	else
    		cout<<ans;
        return 0;
    }
    
  • 相关阅读:
    https 双向证书
    MapReduce概述,原理,执行过程
    rpc,客户端与NameNode通信的过程
    小文件的解决方案
    hadoop hdfs的java操作
    HDFS的shell操作
    HDFS体系结构:(Distributed File System)
    hadoop-1.1.2集群搭建
    Hadoop入门概念
    移动端css知识总结--字体,毛玻璃效果,input和disabled
  • 原文地址:https://www.cnblogs.com/gongdakai/p/11615152.html
Copyright © 2011-2022 走看看