# Question D: dyeing car

Question D: Dye
Time limit: 1 Sec memory limit: 128 MB

Title Description
There are several cars on the n × m chessboard. The two cars attack each other if and only if all the following conditions are met:
1. Two vehicles are in the same line or in the same train.
2. There is no other car between the two cars.
3. The color of the two cars is different.
Now I will give you the location of these cars. Please dye them with as many colors as possible so that no two cars attack each other.
input
The first line read in contains two integers n,m, representing the number of rows and columns of the chessboard.
Next, n rows and m columns describe the whole chessboard.
There are two types of characters in the chessboard. The character "." represents empty space, and the character R represents vehicle.
output
The output is one line in total, indicating the maximum number of colors you can use without attacking each other.
Sample input Copy
3 4
.R.R
R.R.
.R.R
Sample output Copy
2
Tips
For 100% data, 1 ≤ n,m ≤ 20.

```#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define rep1(i,n) for(int i=1;i<=(int)n;i++)
#define redp(i,n) for(int i=(int)n-1;i>=0;i--)
#define redp1(i,n) for(int i=(int)n;i>=1;i--)
#define lowbit(x) (x)&(-x)
#define EPSILON 1e-9
typedef long long ll;
using namespace std;
const int dx[] = { 0, 1, -1, 0, 1, -1, 1, -1 };
const int dy[] = { 1, 0, 0, -1, 1, -1, -1, 1 };
const int M = 1e9 + 7;
const int INF=0x3f3f3f3f;
//const double Pi=acos(-1);
const int MOD = 998244353;
bool flag=false;

//----------------------------------------------
const int N=2e6+10;
//----------------------------------------------
string s,t;
int n,m,k;
char a;
bool v;
int ans=0;

void dfs(int x,int y)
{
v[x][y]=true;  //Mark as stained
rep1(i,n)if(a[i][y]=='R'&&!v[i][y])dfs(i,y);  //Horizontal and vertical
rep1(i,m)if(a[x][i]=='R'&&!v[x][i])dfs(x,i);

}

//----------------------------------------------
int main()
{
int aa,bb,cc;
char c;
memset(v,false,sizeof(v));
cin>>n>>m;

rep1(i,n)rep1(j,m)cin>>a[i][j];

rep1(i,n)rep1(j,m)
{
if(a[i][j]=='R'&&!v[i][j])     //I found the car without color
{
dfs(i,j);
ans++;           //Color + 1
}
}
cout<<ans<<endl;
return 0;
}

```

Posted on Thu, 04 Jun 2020 11:16:34 -0400 by russlock