着色问题
来源:互联网 发布:小学生绘画软件下载 编辑:程序博客网 时间:2024/04/30 19:14
问题描述
输入一个n,m,代表行与列,求用四种颜色填满整个地图,要求两两相邻的不能同色
真是日狗,这道题调试了我一天就是没调试出来,太菜了= = !,最后还是找人调试出来的有木有!
不管怎么说现在调试出来了,但是结果有点多,输入三的时候,结果太多了,然并卵,看代码!
下面是我用两种方法做的,但都差不多,另一种用了哈希算法!
这里写代码片#include<iostream>#include<cstring>#include<conio.h>#define M 1005using 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);}
0 0
- 着色问题
- 多边形着色问题
- m图着色问题
- 区间图着色问题
- 区间图着色问题
- 回溯 图着色问题
- 图的着色问题
- 顶点着色问题应用
- 图的着色问题
- 图着色问题
- 线段树着色问题
- m图着色问题
- 图的着色问题
- 区间图着色问题
- Polya 原理 着色问题
- 图着色问题
- 区间图着色问题
- 图最少着色问题
- [基本实验] 远程桌面攻击
- 飞鸽传书源码分析六-文件传输续
- 为什么中断handler中不能休眠
- Kubernetes技术分析之存储
- close 和shutdown 函数
- 着色问题
- C++中的多态
- 《编程思想-笔记》-第三章,第四章
- servlet 获取 post body 体 (用流读取为空的问题)
- Win10C:\Windows\System32\drivers\etc\hosts提示权限不够问题
- Ubuntu和windows下修改hosts
- uva 784
- 泊松图像编辑(Poisson image editing)
- adt下载地址