Codeforces 445 A DZY Loves Chessboard【DFS】
题意:给出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】的更多相关文章
- codeforces 445 B DZY Loves Chemistry【并查集】
题意:给出n种化学物质,其中m对会发生化学反应,每次加入化学物质进去的时候, 如果有能够和它发生反应的,危险值就乘以2,问怎样的放入顺序使得危险值最大 将这m对会反应的用并查集处理,统计每个连通块里面 ...
- Codeforces 447 C DZY Loves Sequences【DP】
题意:给出一列数,在这个序列里面找到一个连续的严格上升的子串,现在可以任意修改序列里面的一个数,问得到的子串最长是多少 看的题解,自己没有想出来 假设修改的是a[i],那么有三种情况, 1.a[i]& ...
- Codeforces 475 B Strongly Connected City【DFS】
题意:给出n行m列的十字路口,<代表从东向西,>从西向东,v从北向南,^从南向北,问在任意一个十字路口是否都能走到其他任意的十字路口 四个方向搜,搜完之后,判断每个点能够访问的点的数目是否 ...
- Codeforces 510B Fox And Two Dots 【DFS】
好久好久,都没有写过搜索了,看了下最近在CF上有一道DFS水题 = = 数据量很小,爆搜一下也可以过 额外注意的就是防止往回搜索需要做一个判断. Source code: //#pragma comm ...
- codeforces 750D New Year and Fireworks【DFS】
题意:烟花绽放时分为n层,每层会前进ti格,当进入下一层是向左右45°分开前进. 问在网格中,有多少网格至少被烟花经过一次? 题解:最多30层,每层最多前进5格,烟花的活动半径最大为150,每一层的方 ...
- CF444A DZY Loves Physics【结论】
题目传送门 话说这道题不分析样例实在是太亏了...结论题啊... 但是话说回来不知道它是结论题的时候会不会想到猜结论呢...毕竟样例一.二都有些特殊. 观察样例发现选中的子图都只有一条边. 于是猜只有 ...
- (CF)Codeforces445A DZY Loves Chessboard(纯实现题)
转载请注明出处:http://blog.csdn.net/u012860063? viewmode=contents 题目链接:http://codeforces.com/problemset/pro ...
- Codeforces Round #254 (Div. 2) A. DZY Loves Chessboard —— dfs
题目链接: http://codeforces.com/problemset/problem/445/A 题解: 这道题是在现场赛的最后一分钟通过的,相当惊险,而且做的过程也很曲折. 先是用递推,结果 ...
- 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 ...
随机推荐
- Delphi中有序型
有序类型包括:.integer(整型).character(字符型).boolean(布尔型).enumerated(枚举型).subrange(子界型)有序类型定义了一组被排序的值.每个相异值都有唯 ...
- zoj 2358,poj 1775 Sum of Factorials(数学题)
题目poj 题目zoj //我感觉是题目表述不确切,比如他没规定xi能不能重复,比如都用1,那么除了0,都是YES了 //算了,这种题目,百度来的过程,多看看记住就好 //题目意思:判断一个非负整数n ...
- linux使用crontab实现PHP执行定时任务及codeiginter参数传递相关
http://www.phpddt.com/php/linux-crontab.html crontab: yum install crontabs //安装 说明: /sbin/service cr ...
- HashMap的key装换成List
Map<String,Object> map = new HashMap<String,Object>(); map.put("a","32332 ...
- hadoop 2.0 lzo 问题
首先搞到lzo在github https://github.com/cloudera/hadoop-lzo/ ant package 对应的机器上把build目录下的native压缩,传给所有机器 ...
- Java多线程-线程的调度(合并)
线程的合并的含义就是将几个并行线程的线程合并为一个单线程执行,应用场景是当一个线程必须等待另一个线程执行完毕才能执行时可以使用join方法. join为非静态方法,定义如下:void join(): ...
- BufferedReader方法-----Scanner方法
import java.io.*; import java.util.Scanner; public class C { public static void main(String []args) ...
- linux 操作系统下c语言编程入门
2)Linux程序设计入门--进程介绍 3)Linux程序设计入门--文件操作 4)Linux程序设计入门--时间概念 5)Linux程序设计入门--信号处理 6)Linux程序设计入门--消息管理 ...
- CodeForces484A——Bits(贪心算法)
Bits Let's denote as the number of bits set ('1' bits) in the binary representation of the non-negat ...
- BeautifulSoup 安装使用
Linux环境 1. 安装 方法一: 下载:http://www.crummy.com/software/BeautifulSoup/bs4/download/4.2/ 解压:tar -xzvf be ...