1
这一版以为需要对一个点进行上下左右四个方向的搜寻。还是决定使用邻接矩阵存储,虽然邻接表的出边更好搜寻。
class Solution {
public:
static const int N =100;
int cnt=0;
int g[N][N];
typedef pair<int,int> PII;
PII st[N][N];
bool check(int i,int j){
if(!g[i][j-1]){//没坏掉
if(st[i][j-1].first!=-1){//没被push掉
st[i][j]=make_pair(i,j-1);
st[i][j-1]=make_pair(i,j);
cnt++;
return true;
}
else{
if(check(st[i][j-1].first,st[i][j-1].second)){
st[i][j]=make_pair(i,j-1);
cnt++;
st[i][j-1]=make_pair(i,j);
return true;
}
}
}
if(!g[i][j+1]){
if(st[i][j+1].first!=-1){
st[i][j]=make_pair(i,j+1);
st[i][j+1]=make_pair(i,j);
cnt++;
return true;
}
else{
if(check(st[i][j-1].first,st[i][j-1].second)){
st[i][j]=make_pair(i,j+1);
st[i][j+1]=make_pair(i,j);
cnt++;
return true;
}
}
}
if(!g[i-1][j]){
if(st[i-1][j].first!=-1 ){
st[i][j]=make_pair(i-1,j);
st[i-1][j]=make_pair(i,j);
cnt++;
return true;
}
else{
if(check(st[i][j-1].first,st[i][j-1].second)){
st[i-1][j]=make_pair(i,j);
st[i][j]=make_pair(i-1,j);
cnt++;
return true;
}
}
}
if(!g[i+1][j]){
if(st[i+1][j].first!=-1){
st[i][j]=make_pair(i+1,j);
st[i+1][j]=make_pair(i,j);
cnt++;
return true;
}
else{
if(check(st[i][j-1].first,st[i][j-1].second)){
st[i][j]=make_pair(i+1,j);
st[i+1][j]=make_pair(i,j);
cnt++;
return true;
}
}
}
return false;
}
int domino(int n, int m, vector<vector<int>>& broken) {
memset(g,0,sizeof(g));
memset(st,-1,sizeof(st));
int broken_size = broken.size();
for(int i=0;i< broken_size;i++){
int a = broken[i][0];
int b = broken[i][1];
g[a][b] = -1;
g[b][a] = -1;
}
for(int i=1;i<n;i++){
for(int j=1;j<m;j++){
if(!g[i][j]){//没坏掉
bool flag = check(i,j);
}
}
}
return cnt;
}
};
2
这一版更正了搜索思想,其实只需要从左往右,从上往下搜寻就可以了,同时考虑了搜索溢出边界的情况。但是样例通过率是55 / 94 。。。。
class Solution {
public:
static const int N =10;
int cnt=0;
int g[N][N];
bool st[N][N];
typedef pair<int,int> PII;
PII match[N][N];
Solution(){
memset(g,0,sizeof(g));
memset(match,-1,sizeof(match));
}
bool check(int i,int j,int n,int m){
if(!g[i][j+1]&& (j+1)<m)
{
if(!st[i][j+1]){
st[i][j+1]=true;
match[i][j].first = i;
match[i][j].second = j+1;
match[i][j+1].first = i;
match[i][j+1].second = j;
return true;
}
else{
if(check(match[i][j+1].first,match[i][j+1].second,n,m)){
match[i][j].first = i;
match[i][j].second = j+1;
match[i][j+1].first = i;
match[i][j+1].second = j;
return true;
}
}
}
if(!g[i+1][j] && (i+1)<n)
{
if(!st[i+1][j]){
st[i+1][j]=true;
match[i][j].first = i+1;
match[i][j].second = j;
match[i+1][j].first = i;
match[i+1][j].second = j;
return true;
}
else{
if(check(match[i+1][j].first,match[i+1][j].second,n, m)){
match[i][j].first = i+1;
match[i][j].second = j;
match[i+1][j].first = i;
match[i+1][j].second = j;
return true;
}
}
}
return false;
}
int domino(int n, int m, vector<vector<int>>& broken) {
int broken_size = broken.size();
for(int i=0;i< broken_size;i++){
int a = broken[i][0];
int b = broken[i][1];
g[a][b] = -1;
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if((g[i][j]==0) && (match[i][j].first == -1)){//没坏掉
memset(st,false,sizeof st);
bool flag = check(i,j,n,m);
if(flag) cnt++;
}
}
}
return cnt;
}
};
3
n =3
m =2
broken =[[1, 1], [2, 1]]
在这个例子的检查,发现,好像还是得写四个方向,因为在最右列的数值和最下方的数值如果发生冲突,还是需要往回检查的:
g:
0 0
0 -1
0 -1
改用邻接表,根据传统的写法做了:
class Solution {
public:
static const int N =100,M=N*N;
int h[N],e[M],ne[M],idx;
int match[M];
bool st[M];
int stuck[M];
vector<int> starts;
void add(int a,int b){
e[idx] = b;
ne[idx] = h[a];
h[a] = idx++;
}
bool find(int u){
for(int i=h[u];i!=-1;i=ne[i]){
int cur = e[i];
if(stuck[cur]!=-1){
if(!st[cur]){
st[cur]=true;
if(match[cur]==0 || find(match[cur])){
match[cur]=u;
return true;
}
}
}
}
return false;
}
int domino(int n, int m, vector<vector<int>>& broken) {
int broken_size = broken.size();
int dx[4]={0,-1,0,1};
int dy[4]={-1,0,1,0};
int cnt=0;
memset(stuck,0,sizeof(stuck));
memset(h,-1,sizeof(h));
memset(match,0,sizeof(match));
for(int i=0;i< broken_size;i++){
int a = broken[i][0]+1;
int b = broken[i][1]+1;
stuck[a*m+b] = -1;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if((i+j)%2 || stuck[i*m+j]==-1) continue;
stuck[i*m+j] = 1;
starts.push_back(i * m + j);
for(int k=0;k<4;k++){
int x = i + dx[k];
int y = j + dy[k];
if(x<=0 || x>n ||y<=0 || y>m) continue;
if(stuck[x*m+y]==-1) continue;
add(i*m+j,x*m+y);
}
}
}
for(int start: starts) {
memset(st, false, sizeof st);
if(find(start)) cnt++;
}
return cnt;
}
};