poj1753 DFS
2015-11-09 10:36
375 查看
利用DFS暴力枚举,没有什么特别的地方。枚举每一个点的状态。
#include<iostream>
using namespace std;
bool chess[6][6]={false};
int step;
bool flag;
int r[]= {-1,1,0,0,0};
int c[]= {0,0,-1,1,0};
bool judge_all(void)
{
for(int i=1; i<5; i++)
{
for(int j=1; j<5; j++)
{
if(chess[i][j]!=chess[1][1])
{
return false;
}
}
}
return true;
}
void filp(int row,int col)
{
for(int i=0; i<5; i++)
{
chess[row+r[i]][col+c[i]]=!chess[row+r[i]][col+c[i]];
}
return;
}
void dfs(int row,int col,int deep)
{
if(deep==step)
{
flag=judge_all();
return;
}
if(flag||row==5)
return;
filp(row,col);
if(col<4)
dfs(row,col+1,deep+1);
else
dfs(row+1,1,deep+1);
filp(row,col);
if(col<4)
dfs(row,col+1,deep);
else
dfs(row+1,1,deep);
return;
}
int main()
{
char temp;
for(int i=1; i<5; i++)
{
for(int j=1; j<5; j++)
{
cin>>temp;
if(temp=='b')
chess[i][j]=true;
}
}
for(step=0; step<=16; step++)
{
dfs(1,1,0);
if(flag)
{
break;
}
}
if(flag)
printf("%d\n",step);
else
printf("Impossible\n");
}
#include<iostream>
using namespace std;
bool chess[6][6]={false};
int step;
bool flag;
int r[]= {-1,1,0,0,0};
int c[]= {0,0,-1,1,0};
bool judge_all(void)
{
for(int i=1; i<5; i++)
{
for(int j=1; j<5; j++)
{
if(chess[i][j]!=chess[1][1])
{
return false;
}
}
}
return true;
}
void filp(int row,int col)
{
for(int i=0; i<5; i++)
{
chess[row+r[i]][col+c[i]]=!chess[row+r[i]][col+c[i]];
}
return;
}
void dfs(int row,int col,int deep)
{
if(deep==step)
{
flag=judge_all();
return;
}
if(flag||row==5)
return;
filp(row,col);
if(col<4)
dfs(row,col+1,deep+1);
else
dfs(row+1,1,deep+1);
filp(row,col);
if(col<4)
dfs(row,col+1,deep);
else
dfs(row+1,1,deep);
return;
}
int main()
{
char temp;
for(int i=1; i<5; i++)
{
for(int j=1; j<5; j++)
{
cin>>temp;
if(temp=='b')
chess[i][j]=true;
}
}
for(step=0; step<=16; step++)
{
dfs(1,1,0);
if(flag)
{
break;
}
}
if(flag)
printf("%d\n",step);
else
printf("Impossible\n");
}
相关文章推荐
- Java教程:了解Java类的组成
- android SDK manager 无法获取更新版本列表
- C++运算符重载整理2
- 104 自定义大头针
- 笔记 日常的记录
- 关于android方向传感器的使用
- IntentService 串联 按顺序执行(此次任务执行完才执行下一个任务)
- List总结
- 用jQuery做全选
- py爬虫-新浪微博爬虫(1)
- easyui 处理多行的操作基础
- Beaglebone Black(6.1)Beaglebone Black的c++开发环境Eclipse构建
- javaBean与DAO简介
- iOS ARC和MRC混编
- 调用接口传递的XML 及排查原因
- 全卸载Oracle10g 方法(转)
- request doesn't contain a multipart/form-data or multipart/mixed stream
- Cannot open the disk 'F:\centos64-final\CentOS 64-bit\CentOS 64-bit.vmdk' orone of the snapshot disk
- 11月5日全球六大国际域名解析量统计:.US持续下降
- Sum Problem