zoukankan      html  css  js  c++  java
  • 【搜索】四色问题

    原题传送门

    思路


    这道题没什么难的,但卡了本蒟蒻半个小时,因为我忘了在搜索完一种方案并使总方案加一后return......血淋淋的教训啊QAQ,以后肯定不会再忘了......

    Code


    #include<iostream>
    #include<cstdio>
    #include<string>
    #include<vector>
    #include<algorithm>
    #include<cstdlib>
    #include<cmath>
    #include<stack>
    #include<map>
    using namespace std;
    
    int n,e[9][9],b[9],ans;
    
    int r(int no)
    {
    	int i;
    	for(i=1;i<=n;i++)
    	{
    		if(e[no][i]&&b[i]==b[no])
    			return 0;
    	}
    	return 1;
    }
    
    void dfs(int no)
    {
    	if(no==n+1)
    	{
    		ans++;
    		return;
    	}	
    	int i;
    	for(i=1;i<=4;i++)
    	{
    		b[no]=i;
    		if(r(no))
    			dfs(no+1);
    		b[no]=0;
    	}
    }
    
    int main()
    {
        int i,j;
        cin>>n;
        for(i=1;i<=n;i++)
        {
        	for(j=1;j<=n;j++)
        	{
        		cin>>e[i][j];
    		}
    	}
    	dfs(1);
    	cout<<ans<<endl;
        return 0;
    }
    
    
  • 相关阅读:
    分治策略
    uva 11424
    lightoj 1214
    lightoj 1282 && uva 11029
    lightoj 1341
    zoj 2369 Two Cylinders
    几种素数筛法
    lightoj 1245
    hdoj 1299 Diophantus of Alexandria
    求前n项正整数的倒数和
  • 原文地址:https://www.cnblogs.com/gongdakai/p/11443252.html
Copyright © 2011-2022 走看看