• CF 445A DZY Loves Chessboard


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

    DZY loves chessboard, and he enjoys playing with it.

    He has a chessboard of n rows and m columns. Some cells of the chessboard are bad, others are good. For every good cell, DZY wants to put a chessman on it. Each chessman is either white or black. After putting all chessmen, DZY wants that no two chessmen with the same color are on two adjacent cells. Two cells are adjacent if and only if they share a common edge.

    You task is to find any suitable placement of chessmen on the given chessboard.

    Input

    The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 100).

    Each of the next n lines contains a string of m characters: the j-th character of the i-th string is either "." or "-". A "." means that the corresponding cell (in the i-th row and the j-th column) is good, while a "-" means it is bad.

    Output

    Output must contain n lines, each line must contain a string of m characters. The j-th character of the i-th string should be either "W", "B" or "-". Character "W" means the chessman on the cell is white, "B" means it is black, "-" means the cell is a bad cell.

    If multiple answers exist, print any of them. It is guaranteed that at least one answer exists.

    Examples
    input
    Copy
    1 1
    .
    
    output
    Copy
    B
    
    input
    Copy
    2 2
    ..
    ..
    
    output
    Copy
    BW
    WB
    
    input
    Copy
    3 3
    .-.
    ---
    --.
    output
    Copy
    B-B
    ---
    --B
    Note

    In the first sample, DZY puts a single black chessman. Of course putting a white one is also OK.

    In the second sample, all 4 cells are good. No two same chessmen share an edge in the sample output.

    In the third sample, no good cells are adjacent. So you can just put 3 chessmen, no matter what their colors are.

     

    【题意】

    给一个n*m的矩阵,用B和W填充‘.’,且相邻的位置不可以相同。

     

    【分析1】

    看做一个二分图,经典染色法
     

    【代码1】

    #include<cstdio>
    #include<cstdlib>
    using namespace std;
    const int N=105;
    int n,m,ans,dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
    char mp[N][N];
    void dfs(int x,int y,bool t){
    	mp[x][y]=(t?'B':'W');
    	for(int i=0;i<4;i++){
    		int nx=x+dir[i][0];
    		int ny=y+dir[i][1];
    		if(nx<1||ny<1||nx>n||ny>m||mp[nx][ny]!='.') continue;
    		dfs(nx,ny,t^1);
    	}
    }
    inline void Init(){
    	scanf("%d%d",&n,&m);
    	for(int i=1;i<=n;i++) scanf("%s",mp[i]+1);
    }
    inline void Solve(){
    	for(int i=1;i<=n;i++){
    		for(int j=1;j<=m;j++){
    			if(mp[i][j]=='.') dfs(i,j,1);
    		}
    	}
    	for(int i=1;i<=n;i++) puts(mp[i]+1);
    }
    int main(){
    	Init();
    	Solve();
    	return 0;
    } 

     

      

    【分析2】

    规律:若当前位置为’.’,就i + j 时为奇数时输出W,反之输出B;若当前位置为’-’,直接输出
     

    【代码2】

    #include<cstdio>
    #include<cstdlib>
    using namespace std;
    const int N=105;
    int n,m;
    char mp[N][N];
    inline void Init(){
    	scanf("%d%d",&n,&m);
    	for(int i=1;i<=n;i++) scanf("%s",mp[i]+1);
    }
    inline void Solve(){
    	for(int i=1;i<=n;i++){
    		for(int j=1;j<=m;j++){
    			if(mp[i][j]=='-') printf("-");
    			else printf("%s",(i+j)&1?"W":"B");
    		}
    		puts("");
    	}
    }
    int main(){
    	Init();
    	Solve();
    	return 0;
    } 

     

     

     

     

  • 相关阅读:
    habse与Hadoop兼容性问题
    java读取TXT文件(硬核区分编码格式)
    ffmpeg相关用法
    java服务器间http通讯,同时传输文件流和数据,并接收返回的文件流或数据
    js 获取正整数各个位上的数字
    解决Input输入中文重复出现拼音
    为什么 io 包一般以 byte 数组做为处理单位?
    Vue3 + Ts 封装axios
    Vue3 ant design 使用Moment.js日期格式化的实现
    Vue3 NProgress进度条
  • 原文地址:https://www.cnblogs.com/shenben/p/10353259.html
Copyright © 2020-2023  润新知