题意:给出n*m的棋盘,在‘.’处放上B或者W,最后要求所有的B和W都不相邻

先把棋盘的点转化成‘B’,再搜,如果它的四周存在‘B’,则将它变成'W'

一直挂在第五个数据的原因是,没有dfs(nx,ny)

搜索果断弱爆了= =(差不多写了一个小时)

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include <cmath>
  5. #include<stack>
  6. #include<vector>
  7. #include<map>
  8. #include<set>
  9. #include<queue>
  10. #include<algorithm>
  11. using namespace std;
  12.  
  13. typedef long long LL;
  14. const int INF = (<<)-;
  15. const int mod=;
  16. const int maxn=;
  17. char g[][];
  18. int n,m;
  19. int dir[][]={-,,,,,,,-};
  20.  
  21. void dfs(int x,int y){
  22. if(x<||x>n||y<||y>m||g[x][y]=='-') return;
  23. if(g[x][y]=='B'){
  24. for(int i=;i<;i++){
  25. int nx=x+dir[i][];
  26. int ny=y+dir[i][];
  27. if(g[nx][ny]=='B') {
  28. g[nx][ny]='W';
  29. dfs(nx,ny);
  30. }
  31. }
  32. }
  33. if(g[x][y]=='W'){
  34. for(int i=;i<;i++){
  35. int nx=x+dir[i][];
  36. int ny=y+dir[i][];
  37. if(g[nx][ny]=='W') {
  38. g[nx][ny]='B';
  39. dfs(nx,ny);
  40. }
  41. }
  42. }
  43. }
  44.  
  45. int main(){
  46. scanf("%d %d",&n,&m);
  47. for(int i=;i<=n;i++)
  48. for(int j=;j<=m;j++)
  49. cin>>g[i][j];
  50.  
  51. for(int i=;i<=n;i++){
  52. for(int j=;j<=m;j++)
  53. if(g[i][j]=='.') g[i][j]='B';
  54. }
  55.  
  56. for(int i=;i<=n;i++)
  57. for(int j=;j<=m;j++) dfs(i,j);
  58.  
  59. for(int i=;i<=n;i++){
  60. for(int j=;j<=m;j++)
  61. printf("%c",g[i][j]);
  62. printf("\n");
  63. }
  64. return ;
  65. }

后来翻别人的代码来看,发现不用搜索= =判断i+j为奇数,放上'B',否则放上'W'

真是too young---------

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include <cmath>
  5. #include<stack>
  6. #include<vector>
  7. #include<map>
  8. #include<set>
  9. #include<queue>
  10. #include<algorithm>
  11. using namespace std;
  12.  
  13. typedef long long LL;
  14. const int INF = (<<)-;
  15. const int mod=;
  16. const int maxn=;
  17.  
  18. int main(){
  19. int n,m;
  20. char ch;
  21. scanf("%d %d",&n,&m);
  22. for(int i=;i<n;i++){
  23. for(int j=;j<m;j++){
  24. cin>>ch;
  25. if(ch=='.'){
  26. if((i+j)%) cout<<'B';
  27. else cout<<'W';
  28. }
  29. else cout<<'-';
  30. }
  31. printf("\n");
  32. }
  33. return ;
  34. }

加油啊,加油---go--go--go--

Codeforces 445 A DZY Loves Chessboard【DFS】的更多相关文章

  1. codeforces 445 B DZY Loves Chemistry【并查集】

    题意:给出n种化学物质,其中m对会发生化学反应,每次加入化学物质进去的时候, 如果有能够和它发生反应的,危险值就乘以2,问怎样的放入顺序使得危险值最大 将这m对会反应的用并查集处理,统计每个连通块里面 ...

  2. Codeforces 447 C DZY Loves Sequences【DP】

    题意:给出一列数,在这个序列里面找到一个连续的严格上升的子串,现在可以任意修改序列里面的一个数,问得到的子串最长是多少 看的题解,自己没有想出来 假设修改的是a[i],那么有三种情况, 1.a[i]& ...

  3. Codeforces 475 B Strongly Connected City【DFS】

    题意:给出n行m列的十字路口,<代表从东向西,>从西向东,v从北向南,^从南向北,问在任意一个十字路口是否都能走到其他任意的十字路口 四个方向搜,搜完之后,判断每个点能够访问的点的数目是否 ...

  4. Codeforces 510B Fox And Two Dots 【DFS】

    好久好久,都没有写过搜索了,看了下最近在CF上有一道DFS水题 = = 数据量很小,爆搜一下也可以过 额外注意的就是防止往回搜索需要做一个判断. Source code: //#pragma comm ...

  5. codeforces 750D New Year and Fireworks【DFS】

    题意:烟花绽放时分为n层,每层会前进ti格,当进入下一层是向左右45°分开前进. 问在网格中,有多少网格至少被烟花经过一次? 题解:最多30层,每层最多前进5格,烟花的活动半径最大为150,每一层的方 ...

  6. CF444A DZY Loves Physics【结论】

    题目传送门 话说这道题不分析样例实在是太亏了...结论题啊... 但是话说回来不知道它是结论题的时候会不会想到猜结论呢...毕竟样例一.二都有些特殊. 观察样例发现选中的子图都只有一条边. 于是猜只有 ...

  7. (CF)Codeforces445A DZY Loves Chessboard(纯实现题)

    转载请注明出处:http://blog.csdn.net/u012860063? viewmode=contents 题目链接:http://codeforces.com/problemset/pro ...

  8. Codeforces Round #254 (Div. 2) A. DZY Loves Chessboard —— dfs

    题目链接: http://codeforces.com/problemset/problem/445/A 题解: 这道题是在现场赛的最后一分钟通过的,相当惊险,而且做的过程也很曲折. 先是用递推,结果 ...

  9. Codeforces Round #254 (Div. 2):A. DZY Loves Chessboard

    A. DZY Loves Chessboard time limit per test 1 second memory limit per test 256 megabytes input stand ...

随机推荐

  1. Delphi中有序型

    有序类型包括:.integer(整型).character(字符型).boolean(布尔型).enumerated(枚举型).subrange(子界型)有序类型定义了一组被排序的值.每个相异值都有唯 ...

  2. zoj 2358,poj 1775 Sum of Factorials(数学题)

    题目poj 题目zoj //我感觉是题目表述不确切,比如他没规定xi能不能重复,比如都用1,那么除了0,都是YES了 //算了,这种题目,百度来的过程,多看看记住就好 //题目意思:判断一个非负整数n ...

  3. linux使用crontab实现PHP执行定时任务及codeiginter参数传递相关

    http://www.phpddt.com/php/linux-crontab.html crontab: yum install crontabs //安装 说明: /sbin/service cr ...

  4. HashMap的key装换成List

    Map<String,Object> map = new HashMap<String,Object>(); map.put("a","32332 ...

  5. hadoop 2.0 lzo 问题

    首先搞到lzo在github https://github.com/cloudera/hadoop-lzo/   ant package 对应的机器上把build目录下的native压缩,传给所有机器 ...

  6. Java多线程-线程的调度(合并)

    线程的合并的含义就是将几个并行线程的线程合并为一个单线程执行,应用场景是当一个线程必须等待另一个线程执行完毕才能执行时可以使用join方法. join为非静态方法,定义如下:void join(): ...

  7. BufferedReader方法-----Scanner方法

    import java.io.*; import java.util.Scanner; public class C { public static void main(String []args) ...

  8. linux 操作系统下c语言编程入门

    2)Linux程序设计入门--进程介绍 3)Linux程序设计入门--文件操作 4)Linux程序设计入门--时间概念 5)Linux程序设计入门--信号处理 6)Linux程序设计入门--消息管理  ...

  9. CodeForces484A——Bits(贪心算法)

    Bits Let's denote as the number of bits set ('1' bits) in the binary representation of the non-negat ...

  10. BeautifulSoup 安装使用

    Linux环境 1. 安装 方法一: 下载:http://www.crummy.com/software/BeautifulSoup/bs4/download/4.2/ 解压:tar -xzvf be ...