hdu5113-四色定理-搜索&&剪枝&&坑点多多
来源:互联网 发布:免费开源java即时通讯 编辑:程序博客网 时间:2024/05/21 09:42
https://vjudge.net/problem/HDU-5113
给定一个 m*n的图,和k种颜色的数目,要求染色,相邻不能同颜色
搜索+回溯。
坑点:1 在输出的时候,最后一个数字和/n连起来输出就会wa。
(更正:不能这样输出printf(” %d\n”,col[i][j]));
其他就行。。。。后面附上蜜汁wa代码
2 在向上取整时,用ceil就会tle。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>using namespace std;int m,n,k;int col[15][15];int kin[30];bool dfs(int x,int y,int ans){ if(ans==0) return true; for(int i=1;i<=k;i++) if(((ans+1)/2)<kin[i]) return false; for(int j=1;j<=k;j++){ if(kin[j]&&col[x-1][y]!=j&&col[x][y-1]!=j){ kin[j]--; col[x][y]=j; if(y+1<=n) {if(dfs(x,y+1,ans-1)) return true;} else if(dfs(x+1,1,ans-1)) return true; kin[j]++; col[x][y]=0; } } return false;}int main(){ int t; scanf("%d",&t); for(int tt=1;tt<=t;tt++){ scanf("%d%d%d",&m,&n,&k); for(int j=1;j<=k;j++){ scanf("%d",&kin[j]); } memset(col,0,sizeof(col)); printf("Case #%d:\n",tt); if(dfs(1,1,m*n)) { puts("YES"); for(int i=1;i<=m;i++){ for(int j=1;j<=n;j++){ if(j==1) printf("%d",col[i][j]); else printf(" %d",col[i][j]); } printf("\n"); } } else puts("NO"); } return 0;}
wa代码
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>using namespace std;int m,n,k;int col[15][15];int kin[30];bool dfs(int x,int y,int ans){ if(ans==0) return true; for(int i=1;i<=k;i++) if((ans+1)/2<kin[i]) return false; for(int j=1;j<=k;j++){ if(kin[j]&&col[x-1][y]!=j&&col[x][y-1]!=j){ kin[j]--; col[x][y]=j; if(y+1<=n) {if(dfs(x,y+1,ans-1)) return true;} else if(dfs(x+1,1,ans-1)) return true; kin[j]++; col[x][y]=0; } } return false;}int main(){ int t; scanf("%d",&t); for(int tt=1;tt<=t;tt++){ scanf("%d%d%d",&m,&n,&k); for(int j=1;j<=k;j++){ scanf("%d",&kin[j]); } memset(col,0,sizeof(col)); printf("Case #%d:\n",tt); if(dfs(1,1,m*n)) { puts("YES"); for(int i=1;i<=m;i++){ for(int j=1;j<=n;j++){ if(j==1) printf("%d",col[i][j]); else if(j==n) { printf(" %d\n",col[i][j]); //cout<<endl; }else printf(" %d",col[i][j]); } //printf("\n"); } } else puts("NO"); } return 0;}
阅读全文
0 0
- hdu5113-四色定理-搜索&&剪枝&&坑点多多
- hdu5113 剪枝搜索
- hdu5113(dfs+剪枝)
- HDU5113【DFS+剪枝】
- hdu 5113 我不是四色定理 dfs+剪枝
- hdu5113 Black And White(dfs+剪枝)
- hdu5113 Black And White--DFS & 剪枝
- hdu5113 Black And White(DFS+剪枝)
- hdu5113 Black And White DFS+剪枝
- POJ 1129Channel Allocation(四色+dfs搜索剪枝)
- POJ 1129:Channel Allocation 四色定理+暴力搜索
- POJ-1129-Channel Allocation-dfs搜索+四色定理
- (POJ1129)Channel Allocation <涂色问题问最少颜色数 剪枝搜索 > || <四色定律>
- 搜索剪枝
- 搜索剪枝
- 搜索 剪枝
- 搜索剪枝
- 搜索剪枝
- hiho一下 第156周 岛屿
- suse12: docker 限制内存, 报your kernel not support swap limit capabilities,memory limited without swap
- os——可移植的访问操作系统的特定功能
- 设计模式--单例模式--Java实现
- android 修改项目名称
- hdu5113-四色定理-搜索&&剪枝&&坑点多多
- Linux系统改变ls文件和文件夹颜色方法
- IDEA版本控制的文件颜色
- 架构、框架、设计模式简述
- 警告:不能通过‘...’传递有非简单旧数据类型‘struct std::string’的对象
- ORACLE的别名不能进行运算
- web服务器tomcat入门实战
- Java中Map根据键值(key)或者值(value)进行排序实现
- 使用pygraphviz画出二叉树