地图填色问题

 

地图填色
Time Limit:3000MS  Memory Limit:65536K
Total Submit:31 Accepted:10
Description
我们知道,一张地图使用不同的颜色标注相邻的省份的(废话……不然就乱了……)。但是我们想知道用n种颜色对m个省填色共有多少种方法。
当然,我们会给出这m个省得相邻情况。
Input
第一行为两个整数n m表示颜色与省份数量。(n,m<=20)
第二行有一个整数k表示有k个相邻关系。(k< m*n)
后面k行每行有两个数字x y表示第x省与第y省相邻。
Output
输出一行整数,表示可能的填充方法数。
Sample Input
4 4
4
1 2
1 4
2 3
3 4
Sample Output
84
Hint
输出结果不超过10^8
Source
 

#include<iostream>
using namespace std;

int n,m;
long total=0;
int data[401][401];
int color[21];

bool colorsame(int pt)
{
 int i;
 for(i=1;i<=pt-1;i++)
 {
  if(data[i][pt]==1)
   {
    if(color[i]==color[pt]) return true;
   }
 }
 return false;
}

void trycolor(int pt)
{
 int i;
 if(pt>m)
 {
  //for(i=1;i<=m;i++)
   //cout<<color[i]<<" ";
  //cout<<endl;
  total++; 
 }
 else
 {
  for(i=1;i<=n;i++)
  {
   color[pt]=i;
   if(colorsame(pt)==false)
   trycolor(pt+1);
  }
 }
}

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    int k;
 int x,y;
 int i,j;

 cin>>n>>m;
 cin>>k;
 for(i=0;i<401;i++)
  for(j=0;j<401;j++)
   data[i][j]=0;

 for(i=0;i<=m;i++)
  color[i]=0;

 for(i=0;i<k;i++)
 {
  cin>>x>>y;
  data[x][y]=1;
 }

 trycolor(1);
 cout<<total<<endl;
 //system("pause");
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值