着色问题

问题描述
输入一个n,m,代表行与列,求用四种颜色填满整个地图,要求两两相邻的不能同色
真是日狗,这道题调试了我一天就是没调试出来,太菜了= = !,最后还是找人调试出来的有木有!
不管怎么说现在调试出来了,但是结果有点多,输入三的时候,结果太多了,然并卵,看代码!
下面是我用两种方法做的,但都差不多,另一种用了哈希算法!

这里写代码片
#include<iostream>
#include<cstring>
#include<conio.h>
#define M 1005
using namespace std;
bool vis[M][M];
int c[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
int map[M][M];
int col[4]={0,1,2,3};
char color[4][10]={"red","blue","yellow","black"};
int n,m;
int s;
void Dfs(int w,int r,int num){
    if(num==n*m){
        cout<<"Number:"<<s<<endl;
        s++;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++)
                for(int k=0;k<4;k++)
                    if(k==map[i][j])
                    cout<<color[k]<<"     ";
            cout<<endl;
        }
        cout<<endl;
        return;
    }
    for(int i=0;i<4;i++){
        int w_1=w+c[i][0];
        int r_1=r+c[i][1];
        if(w_1>=n||w_1<0||r_1>=m||r_1<0||vis[w_1][r_1])
            continue;
        for(int j=0;j<4;j++){
            int flag=0;
            for(int k=0;k<4;k++){
                int w_2=w_1+c[k][0];
                int r_2=r_1+c[k][1];
                if(w_2<0||w_2>=n||r_2<0||r_2>=m||!vis[w_2][r_2]){
                    continue;
                }
                if(map[w_2][r_2]==col[j]){
                    flag=1;
                    break;
                }
            }
            if(!flag){
                map[w_1][r_1]=col[j];
                vis[w_1][r_1]=true;
                Dfs(w_1,r_1,num+1);
                vis[w_1][r_1]=false;
            }
        }
    }
}
int main()
{
    while(cin>>n>>m)
    {
        memset(vis,false,sizeof(vis));
        s=0;
        for(int i=0;i<4;i++)
        {
            map[0][0]=col[i];
            vis[0][0]=true;
            Dfs(0,0,1);
            vis[0][0]=false;
        }
    }
    return 0;
}
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int Graph[100][100];
int X[4]= {0,1,0,-1};
int Y[4]= {1,0,-1,0};
int m,n;
void dfs(int x,int y,int num)
{
    if(num==n*m-1)
    {
        bool Hash[5];
        memset(Hash,true,sizeof(Hash));
        for(int i=0; i<4; i++)
        {
            if(x+X[i]>=0 && x+X[i]<n && y+Y[i]>=0 && y+Y[i]<m && Graph[x+X[i]][y+Y[i]])
                Hash[Graph[x+X[i]][y+Y[i]]]=false;
        }
        for(int i=1; i<=4; i++)
            if(Hash[i])
            {
                Graph[x][y]=i;
                for(int i=0; i<n; i++)
                {
                    for(int j=0; j<m; j++)
                        cout<<Graph[i][j];
                    cout<<endl;
                }
                cout<<endl;
            }
        Graph[x][y]=0;
    }
    if(num<n*m)
    {
        bool Hash[5];
        memset(Hash,true,sizeof(Hash));
        for(int i=0; i<4; i++)
        {
            if(x+X[i]>=0 && x+X[i]<n && y+Y[i]>=0 && y+Y[i]<m && Graph[x+X[i]][y+Y[i]])
                Hash[Graph[x+X[i]][y+Y[i]]]=false;
        }
        for(int i=1; i<=4; i++)
            if(Hash[i])
            {
                Graph[x][y]=i;
                for(int i=0; i<4; i++)
                {
                    if(x+X[i]>=0 && x+X[i]<n && y+Y[i]>=0 && y+Y[i]<m && !Graph[x+X[i]][y+Y[i]])
                    {
                        dfs(x+X[i],y+Y[i],num+1);
                        Graph[x+X[i]][y+Y[i]]=0;
                    }
                }
            }
    }
    return;
}
int main()
{
    cin>>m>>n;
    memset(Graph,0,sizeof(Graph));
    dfs(0,0,0);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值