For the first time I feel that I should use DFS to solve this problem, but because I am not too familiar with DFS, so I gave up the idea;
But also want to follow this requirement to achieve the problem must be black and white, and then the character is ' B ' or ' W ' instead of '-', can be;
Let's stick to my code.
1#include <cstdio>2#include <cstring>3#include <algorithm>4 #defineN 100+105 using namespacestd;6 7 CharMaze[n][n];8 CharStr[n][n];9 intn,m;Ten One intMainvoid) A { - while(~SCANF ("%d%d",&n,&m)) - { thememset (str,0,sizeof(str)); - for(intI=0; i<n;++i) -scanf"%s", Maze[i]); - for(intI=0; i<n;++i) + for(intj=0; j<m;++j) { - if(maze[i][j]=='-') +STR[I][J] ='-'; A Else{ at if((i+j)%2==1) str[i][j]='B'; - Elsestr[i][j]='W'; - } - } - for(intI=0; i<n;++i) { - for(intj=0; j<m;++j) inprintf"%c", Str[i][j]); -printf"\ n"); to } + } - the return 0; *}
I will then insert the code I copied from the other great god blogs about the DFS solution:
1#include <cstdio>2#include <iostream>3#include <algorithm>4 using namespacestd;5 6 intn,m;7 Charmap[ the][ the];8 intdir[][2]={{0,1},{0,-1},{-1,0},{1,0}};9 voidDFS (intXintYintnum)Ten { One if(num==0) A { -map[x][y]='W'; - } the Else - { -map[x][y]='B'; - } + intFx,fy; - for(intI=0;i<4; i++) + { Afx=x+dir[i][0]; atfy=y+dir[i][1]; - if(fx>=0&&fx<n&&fy>=0&&fy<m&&map[fx][fy]=='.') - { - if(num==0) - { -DFS (Fx,fy,1); in } - Else to { +DFS (Fx,fy,0); - } the } * } $ }Panax Notoginseng intMain () - { thescanf"%d%d",&n,&m); + for(intI=0; i<n;i++) A { thescanf"%s", Map[i]); + } - for(intI=0; i<n;i++) $ { $ for(intj=0; j<m;j++) - { - if(map[i][j]=='.') the { -DFS (I,j,0);Wuyi } the } - } Wu for(intI=0; i<n;i++) - { Aboutprintf"%s\n", Map[i]); $ } - return 0; -}
Codeforces-445a-dzy Loves Chessboard Problem Solving report