第二次写博客…
D. Bishwock
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Bishwock is a chess figure that consists of three squares resembling an “L-bar”. This figure can be rotated by 90, 180 and 270 degrees so it can have four possible states:
XX XX .X X.
X. .X XX XX
Bishwocks don’t attack any squares and can even occupy on the adjacent squares as long as they don’t occupy the same square.
Vasya has a board with 2×n squares onto which he wants to put some bishwocks. To his dismay, several squares on this board are already occupied by pawns and Vasya can’t put bishwocks there. However, pawns also don’t attack bishwocks and they can occupy adjacent squares peacefully.
Knowing the positions of pawns on the board, help Vasya to determine the maximum amount of bishwocks he can put onto the board so that they wouldn’t occupy the same squares and wouldn’t occupy squares with pawns.
Input
The input contains two nonempty strings that describe Vasya’s board. Those strings contain only symbols “0” (zero) that denote the empty squares and symbols “X” (uppercase English letter) that denote the squares occupied by pawns. Strings are nonempty and are of the same length that does not exceed 100.
Output
Output a single integer — the maximum amount of bishwocks that can be placed onto the given board.
Examples
inputCopy
00
00
outputCopy
1
inputCopy
00X00X0XXX0
0XXX0X00X00
outputCopy
4
inputCopy
0X0X0
0X0X0
outputCopy
0
inputCopy
0XXX0
00000
outputCopy
2
题解
我太菜了,第一个想到的思路是用dfs,然后就随便写写,竟然一遍过了。
#include<bits/stdc++.h>
using namespace std;
char str[2][105];
int vis[2][205],len,maxx=0;
void dfs(int x,int step){
if(x>=len){
if(maxx<step)
maxx=step;
return;
}
if(str[0][x]=='0'&&str[0][x+1]=='0'&&str[1][x]=='0'&&vis[0][x]==0&&vis[0][x+1]==0&&vis[1][x]==0){
vis[0][x]=1,vis[0][x+1]=1,vis[1][x]=1;
dfs(x+1,step+1);
vis[0][x]=0,vis[0][x+1]=0,vis[1][x]=0;
}
else if(str[0][x]=='0'&&str[0][x+1]=='0'&&str[1][x+1]=='0'&&vis[0][x]==0&&vis[0][x+1]==0&&vis[1][x+1]==0){
vis[0][x]=1,vis[0][x+1]=1,vis[1][x+1]=1;
dfs(x+1,step+1);
vis[0][x]=0,vis[0][x+1]=0,vis[1][x+1]=0;
}
else if(str[0][x]=='0'&&str[1][x+1]=='0'&&str[1][x]=='0'&&vis[0][x]==0&&vis[1][x+1]==0&&vis[1][x]==0){
vis[0][x]=1,vis[1][x+1]=1,vis[1][x]=1;
dfs(x+1,step+1);
vis[0][x]=0,vis[1][x+1]=0,vis[1][x]=0;
}
else if(str[0][x+1]=='0'&&str[1][x+1]=='0'&&str[1][x]=='0'&&vis[0][x+1]==0&&vis[1][x+1]==0&&vis[1][x]==0){
vis[0][x+1]=1,vis[1][x+1]=1,vis[1][x]=1;
dfs(x+1,step+1);
vis[0][x+1]=0,vis[1][x+1]=0,vis[1][x]=0;
}
else
dfs(x+1,step);
}
int main(){
cin>>str[0]>>str[1];
len=strlen(str[0]);
dfs(0,0);
cout<<maxx;
return 0;
}