DFS 训练题
来源:互联网 发布:linux中tar命令 编辑:程序博客网 时间:2024/05/29 14:40
Description
- Choose any one of the 16 pieces.
- Flip the chosen piece and also all adjacent pieces to the left, to the right, to the top, and to the bottom of the chosen piece (if there are any).
Consider the following position as an example:
bwbw
wwww
bbwb
bwwb
Here "b" denotes pieces lying their black side up and "w" denotes pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will become:
bwbw
bwww
wwwb
wwwb
The goal of the game is to flip either all pieces white side up or all pieces black side up. You are to write a program that will search for the minimum number of rounds needed to achieve this goal.
Input
Output
Sample Input
bwwbbbwbbwwbbwww
Sample Output
4
题意:翻动其中的一个棋子,旁边的棋子也会跟着改变颜色,目标是全盘白或者是全盘黑~
WAY:DFS+枚举...DFS弄了很才弄懂TT...弄懂之后发现没什么好说的了~
MY CODE:
#include<stdio.h>#include<iostream>#include<algorithm>using namespace std;const int M=32;bool map2[5][5];int s,i,j,cur,ans;bool finish(){ for(i=0;i<4;i++) for(j=0;j<4;j++) { if(map2[i][j]!=map2[0][0]) return 0; } return 1;}void change(int cur){ int x,y; x=cur/4; y=cur%4; map2[x][y]=!map2[x][y]; map2[x+1][y]=!map2[x+1][y]; if(x-1>=0) {map2[x-1][y]=!map2[x-1][y];} if(y-1>=0) {map2[x][y-1]=!map2[x][y-1];} map2[x][y+1]=!map2[x][y+1];}void dfs(int cur,int s){ if(finish()) {ans=min(ans,s);return ;} if(cur>15) return ; dfs(cur+1,s); change(cur); dfs(cur+1,s+1); change(cur);}int main(){ char map; for(i=0;i<4;i++) { for(j=0;j<4;j++) { scanf("%c",&map); if(map=='b')map2[i][j]=1; if(map=='w')map2[i][j]=0; } getchar(); } ans=M; dfs(0,0); if(ans!=M)printf("%d\n",ans); else printf("Impossible\n"); return 0;}
Description
The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to open a refrigerator.
There are 16 handles on the refrigerator door. Every handle can be in one of two states: open or closed. The refrigerator is open only when all handles are open. The handles are represented as a matrix 4х4. You can change the state of a handle in any location [i, j] (1 ≤ i, j ≤ 4). However, this also changes states of all handles in row i and all handles in column j.
The task is to determine the minimum number of handle switching necessary to open the refrigerator.
Input
The input contains four lines. Each of the four lines contains four characters describing the initial state of appropriate handles. A symbol “+” means that the handle is in closed state, whereas the symbol “−” means “open”. At least one of the handles is initially closed.
Output
The first line of the input contains N – the minimum number of switching. The rest N lines describe switching sequence. Each of the lines contains a row number and a column number of the matrix separated by one or more spaces. If there are several solutions, you may give any one of them.
Sample Input
-+-----------+--
Sample Output
61 11 31 44 14 34 4
题意:改变其中一个纽扣的状态,所在的那行那列也会改变状态,目标是全部都开,即' - '~
WAY:和上题极像极像,也是枚举+DFS....但是在储存纽扣位置上折腾了很久..应该先储存在临时数组上,如果完成才转换在用于输出的数组上~
MY CODE:
#include <stdio.h>#include <iostream>#include <algorithm>using namespace std;const int M=32;bool map2[5][5];int x[20],y[20];//临时int ansx[20],ansy[20];int s,i,j,cur,ans,k=0;bool finish(){ for(i=0;i<4;i++) for(j=0;j<4;j++) { if(map2[i][j]!=1) return 0; } return 1;}void change(int cur){ int x,y; x=cur/4; y=cur%4; for(int i=0;i<4;i++) { map2[x][i]=!map2[x][i]; map2[i][y]=!map2[i][y]; } map2[x][y]=!map2[x][y];}void dfs(int cur,int s){ if(finish()) { if(ans>s) { ans=s; for(i=1;i<=ans;i++) { ansx[i]=x[i]; ansy[i]=y[i]; } } return ; } if(cur>15)return ; dfs(cur+1,s); change(cur); x[s+1]=cur/4+1; y[s+1]=cur%4+1; dfs(cur+1,s+1); change(cur);}int main(){ char map; for(i=0;i<4;i++) { for(j=0;j<4;j++) { scanf("%c",&map); if(map=='-')map2[i][j]=1; if(map=='+')map2[i][j]=0; } getchar(); } ans=M; dfs(0,0); printf("%d\n",ans); for(i=1;i<=ans;i++) { printf("%d %d\n",ansx[i],ansy[i]); } return 0;}
附上: 神牛解法
参考高手的高效解法:> 证明:要使一个为'+'的符号变为'-',必须其相应的行和列的操作数为奇数;可以证明,如果'+'位置对应的行和列上每一个位置都进行一次操作,则整个图只有这一'+'位置的符号改变,其余都不会改变.> 设置一个4*4的整型数组,初值为零,用于记录每个点的操作数,那么在每个'+'上的行和列的的位置都加1,得到结果模2(因为一个点进行偶数次操作的效果和没进行操作一样,这就是楼上说的取反的原理),然后计算整型数组中一的> 个数即为操作数,一的位置为要操作的位置(其他原来操作数为偶数的因为操作并不发生效果,因此不进行操作)
#include <iostream>using namespace std;bool mark[4][4];char s[4][4];int main(){ int i,j,k; int ci[16],cj[16]; int nas = 0; memset(mark,0,sizeof(mark));for(i = 0;i < 4;i++)cin >> s[i]; for(i = 0;i < 4;i++) for(j = 0;j < 4;j++) { char c = s[i][j]; if(c == '+') { mark[i][j] = !mark[i][j]; for(k = 0;k < 4;k++) { mark[i][k] = !mark[i][k]; mark[k][j] = !mark[k][j]; } } } for(i = 0;i < 4;i++) for(j = 0;j < 4;j++) if(mark[i][j] == true) { ci[nas] = i + 1; cj[nas] = j + 1; nas ++; } printf("%d\n",nas); for(i = 0;i < nas;i++) { printf("%d %d\n",ci[i],cj[i]); } return 0;}
- DFS 训练题
- DFS/BFS 搜索训练
- 暑假集训--训练2 DFS
- 7.22BFS&DFS训练计划
- 2014年1月14日训练赛D题题解(栈+dfs)
- DFS-BFS搜索专题【经典训练题】【有时间一个个做下来】
- DFS-BFS搜索专题【经典训练题】【有时间一个个做下来】
- 寒假训练--dfs+bfs--救基友记3
- 寒假训练--dfs+bfs--找朋友
- 【DFS训练】wiki题目之 传话
- POJ训练计划3009_Curling 2.0(DFS)
- 算法训练 最大的算式 【dfs】
- CSU-ACM2016暑假集训训练2-DFS
- 蓝桥杯 警察智力训练(DFS)
- 算法训练 和为T dfs
- CSU-ACM2017暑期训练4-dfs D
- CSU-ACM2017暑期训练4-dfs E
- CSU-ACM2017暑期训练4-dfs F
- 【iOS7的一些总结】5、iOS中的内存管理
- c++中的动态识别
- 小小文本框,大大的逻辑
- Oracle Installation Type
- POJ 2155 Matrix【二维线段树】
- DFS 训练题
- Java 构造器和方法的区别
- 总结蛇尾,展望马首
- [DFS]FJSDFZOJ 1079/NOIP2002 选数
- Visual C#2010从入门到精通读书笔记1~6章
- 沧海一声笑
- 如何对PHP程序中的常见漏洞进行攻击
- A计划之读书篇(二月)
- servlet (2) 生命周期