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

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

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

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

 #include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
using namespace std; typedef long long LL;
const int INF = (<<)-;
const int mod=;
const int maxn=;
char g[][];
int n,m;
int dir[][]={-,,,,,,,-}; void dfs(int x,int y){
if(x<||x>n||y<||y>m||g[x][y]=='-') return;
if(g[x][y]=='B'){
for(int i=;i<;i++){
int nx=x+dir[i][];
int ny=y+dir[i][];
if(g[nx][ny]=='B') {
g[nx][ny]='W';
dfs(nx,ny);
}
}
}
if(g[x][y]=='W'){
for(int i=;i<;i++){
int nx=x+dir[i][];
int ny=y+dir[i][];
if(g[nx][ny]=='W') {
g[nx][ny]='B';
dfs(nx,ny);
}
}
}
} int main(){
scanf("%d %d",&n,&m);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
cin>>g[i][j]; for(int i=;i<=n;i++){
for(int j=;j<=m;j++)
if(g[i][j]=='.') g[i][j]='B';
} for(int i=;i<=n;i++)
for(int j=;j<=m;j++) dfs(i,j); for(int i=;i<=n;i++){
for(int j=;j<=m;j++)
printf("%c",g[i][j]);
printf("\n");
}
return ;
}

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

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

 #include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
using namespace std; typedef long long LL;
const int INF = (<<)-;
const int mod=;
const int maxn=; int main(){
int n,m;
char ch;
scanf("%d %d",&n,&m);
for(int i=;i<n;i++){
for(int j=;j<m;j++){
cin>>ch;
if(ch=='.'){
if((i+j)%) cout<<'B';
else cout<<'W';
}
else cout<<'-';
}
printf("\n");
}
return ;
}

加油啊,加油---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. 迁移到MariaDB galera

    迁移到MariaDB galera [已注销] [已注销] -- :: [安装] ====== https://downloads.mariadb.org/mariadb/repositories/ ...

  2. Android 4.4KitKat AudioRecord 流程分析

    Android是架构分为三层: 底层      Linux Kernel 中间层  主要由C++实现 (Android 60%源码都是C++实现) 应用层  主要由JAVA开发的应用程序 应用程序执行 ...

  3. Linux的别名使用

    直接定义别名 编辑当前用户下的.bashrc 文件: vim  ~/.bashrc 添加别名为 lmysql 的命令语句 : alias lmysql='mysql -uroot -p -Dtest ...

  4. ExtJs之Ext.util.ClickRepeater

    <!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...

  5. lintcode:将二叉树拆成链表

    题目 将一棵二叉树按照前序遍历拆解成为一个假链表.所谓的假链表是说,用二叉树的 right 指针,来表示链表中的 next 指针. 注意事项 不要忘记将左儿子标记为 null,否则你可能会得到空间溢出 ...

  6. lintcode:两数组的交 II

    题目 计算两个数组的交 注意事项 每个元素出现次数得和在数组里一样答案可以以任意顺序给出 样例 nums1 = [1, 2, 2, 1], nums2 = [2, 2], 返回 [2, 2]. 解题 ...

  7. lintcode:买卖股票的最佳时机 IV

    买卖股票的最佳时机 IV 假设你有一个数组,它的第i个元素是一支给定的股票在第i天的价格. 设计一个算法来找到最大的利润.你最多可以完成 k 笔交易. 注意事项 你不可以同时参与多笔交易(你必须在再次 ...

  8. TCP三次握手和四次挥手协议

    相对于SOCKET开发者,TCP创建过程和链接折除过程是由TCP/IP协议栈自动创建的.因此开发者并不需要控制这个过程.但是对于理解TCP底层运作机制,相当有帮助. TCP三次握手   所谓三次握手( ...

  9. Cloudinsight Agent install script

    #!/bin/bash # Cloudinsight Agent install script. set -e logfile="ci-agent-install.log" gis ...

  10. IOS底层数据结构--class

    一.类的数据结构 Class(指针) typedef struct objc_class *Class; /* 这是由编译器为每个类产生的数据结构,这个结构定义了一个类.这个结构是通过编译器在执行时产 ...