题目
样例
第一次做连连看题,其实就是dfs找路,只要三步之内能消除就算成功,并且只需要考虑一下特殊的情况就算ok了
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int mp[200][200];
int n,m;
int sx,sy;
int dir[4][2]={{1,0},{0,1},{0,-1},{-1,0}};//分别对应下,右,左,上
int check(int x,int y){
if(x<0 || x>n+1 || y<0 || y>m+1)
return 1;
return 0;
}
bool judge(int x,int y,int step,int pos){//pos表示上一步方向
if(step>3)
return 0;//如果超过了三步,不符合规则
if(mp[x][y]==mp[sx][sy] && pos!=-1){//如果两个字符相等并且不是同一个(由于下面有方向限制,所以两个值不可能相等)
mp[x][y]=0;//删去配对字符
mp[sx][sy]=0;
return 1;
}
if(mp[x][y]!=0 && pos!=-1)
return 0;//如果不相等并且不是通路,不符合规则
int i,x1,y1;
for(i=0;i<4;i++){
if(i+pos==3) continue;//不能有正相反的方向 (0.下 3.上) (1.右 2.左)
x1=x+dir[i][0];
y1=y+dir[i][1];
if(check(x1,y1)) continue;//检查是否越界
if(judge(x1,y1,step+(pos==i?0:1),i)){//找到一个就返回
return 1;
}
}
return 0;
}
int main()
{
int t,times,sum;
int i,j;
scanf("%d",&t);
while(t--){
scanf("%d %d",&n,&m);
memset(mp,0,sizeof(mp));
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
scanf("%d",&mp[i][j]);
}
}
sum=0;
times=0;//times表示查找的次数,大于等于n*m相当于查找一遍还没有找到
i=j=1;
while(sum<n*m && times<n*m){
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
times++;
sx=i,sy=j;
if(mp[i][j]!=0 && judge(i,j,0,-1)){
sum+=2;
times=0;
}
}
}
}
if(sum==n*m){
printf("Yes\n");
}
else{
printf("No\n");
}
}
return 0;
}
但是这次比赛事后加了数据用这个代码会超时,给的新的标称没看明白,先放着,以后再想想
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define ll long long
using namespace std;
/*************************************************
Program: Fast I/O System
Author: zdw1999
Last Update: 2018-12-15 22:15
Usage:
init_num_table(); //at begin
(FastInput) IN.file_init(stdin); //at begin
(FastInput) OUT.flush(stdout); //at end
*************************************************/
#ifndef MY_FASTIO_H
#define MY_FASTIO_H
namespace zdw{
void readint(int &x){
char s=getchar(),c=0;
while(~s&&s<'0'||s>'9')c=s,s=getchar(); x=s-'0';
while(~(s=getchar())&&s>='0'&&s<='9')x=(x<<3)+(x<<1)+s-'0';
if(c=='-')x=-x;
}
void readll(ll &x){
char s=getchar(),c=0;
while(~s&&s<'0'||s>'9')c=s,s=getchar(); x=s-'0';
while(~(s=getchar())&&s>='0'&&s<='9')x=(x<<3)+(x<<1)+s-'0';
if(c=='-')x=-x;
}
void _readint(char *&s,int &x){
char w=*s,c;
while(w&&w<'0'||w>'9')w=*++s;
if(!w){x=-1;return;} c=*(s-1); x=w-'0';
while((w=*++s)&&w>='0'&&w<='9')x=(x<<3)+(x<<1)+w-'0';
if(c=='-')x=-x;
}
void _readll(char *&s,ll &x){
char w=*s,c;
while(w&&w<'0'||w>'9')w=*++s;
if(!w){x=-1;return;} c=*(s-1); x=w-'0';
while((w=*++s)&&w>='0'&&w<='9')x=(x<<3)+(x<<1)+w-'0';
if(c=='-')x=-x;
}
void readintmod(int &x,const int &ha){
char s=getchar(),c=0;
while(~s&&s<'0'||s>'9')c=s,s=getchar(); x=s-'0';
while(~(s=getchar())&&s>='0'&&s<='9'){
x=(x<<3)+(x<<1)+s-'0';
if(x>2e8)x%=ha;
}
if(c=='-')x=-x;
}
void readllmod(ll &x,const ll &ha){
char s=getchar(),c=0;
while(~s&&s<'0'||s>'9')c=s,s=getchar(); x=s-'0';
while(~(s=getchar())&&s>='0'&&s<='9'){
x=(x<<3)+(x<<1)+s-'0';
if(x>9e17)x%=ha;
}if(x>=ha)x%=ha;
if(c=='-')x=-x;
}
void _readintmod(char *&s,int &x,const int &ha){
char w=*s,c;
while(w&&w<'0'||w>'9')w=*++s;
if(!w){x=-1;return;} c=*(s-1); x=w-'0';
while((w=*++s)&&w>='0'&&w<='9'){
x=(x<<3)+(x<<1)+w-'0';
if(x>2e8)x%=ha;
}
if(c=='-')x=-x;
}
void _readllmod(char *&s,ll &x,const ll &ha){
char w=*s,c;
while(w&&w<'0'||w>'9')w=*++s;
if(!w){x=-1;return;} c=*(s-1); x=w-'0';
while((w=*++s)&&w>='0'&&w<='9'){
x=(x<<3)+(x<<1)+w-'0';
if(x>9e17)x%=ha;
}if(x>=ha)x%=ha;
if(c=='-')x=-x;
}
/*
void _putint(const int &x){int t=x/10;if(t)_putint(t);putchar(x-t*10+'0');}
void putint(const int &x){x<0?putchar('-'),_putint(-x):_putint(x);}
void _putll(const ll &x){ll t=x/10;if(t)_putll(t);putchar(x-t*10+'0');}
void putll(const ll &x){x<0?putchar('-'),_putll(-x):_putll(x);}
void __putint(char *&s,const int &x){int t=x/10;if(t)__putint(s,t);*s++=x-t*10+'0';}
void _putint(char *&s,const int &x){x<0?*s++='-',__putint(s,-x):__putint(s,x);}
void __putll(char *&s,const ll &x){ll t=x/10;if(t)__putll(s,t);*s++=x-t*10+'0';}
void _putll(char *&s,const ll &x){x<0?*s++='-',__putll(s,-x):__putll(s,x);}
*/
char __num_table[500001];
void init_num_table(){
for(int i=0;i<100000;++i){
//sprintf(__num_table+i*5,"%05d",i);
char *t=__num_table+i*5;
t[0]=i/10000+'0';
t[1]=(i/1000)%10+'0';
t[2]=(i/100)%10+'0';
t[3]=(i/10)%10+'0';
t[4]=i%10+'0';
}
}
void putint(int x){
if(x<0)putchar('-'),x=-x;
if(x<=9){putchar(x+'0');return;}
char w[11],*p=w,*t; *p=0;
int z;
while(x>9999){
z=x/100000; t=__num_table+(x-z*100000)*5;
p[1]=t[4]; p[2]=t[3]; p[3]=t[2]; p[4]=t[1]; p[5]=t[0];
x=z; p+=5;
}t=__num_table+x*5;
t[1]>'0'?p[1]=t[4],p[2]=t[3],p[3]=t[2],p[4]=t[1],p+=4,0:
t[2]>'0'?p[1]=t[4],p[2]=t[3],p[3]=t[2],p+=3,0:
t[3]>'0'?p[1]=t[4],p[2]=t[3],p+=2,0:
t[4]>'0'?*++p=t[4],0:0;
while(*p)putchar(*p--);
}
void putll(ll x){
if(x<0)putchar('-'),x=-x;
if(x<=9){putchar(x+'0');return;}
char w[20],*p=w,*t; *p=0;
ll z;
while(x>9999){
z=x/100000; t=__num_table+(x-z*100000)*5;
p[1]=t[4]; p[2]=t[3]; p[3]=t[2]; p[4]=t[1]; p[5]=t[0];
x=z; p+=5;
}t=__num_table+x*5;
t[1]>'0'?p[1]=t[4],p[2]=t[3],p[3]=t[2],p[4]=t[1],p+=4,0:
t[2]>'0'?p[1]=t[4],p[2]=t[3],p[3]=t[2],p+=3,0:
t[3]>'0'?p[1]=t[4],p[2]=t[3],p+=2,0:
t[4]>'0'?*++p=t[4],0:0;
while(*p)putchar(*p--);
}
void _putint(char *&s,int x){
if(x<0)*s++='-',x=-x;
if(x<=9){*s++=x+'0';return;}
char w[11],*p=w,*t; *p=0;
int z;
while(x>9999){
z=x/100000; t=__num_table+(x-z*100000)*5;
p[1]=t[4]; p[2]=t[3]; p[3]=t[2]; p[4]=t[1]; p[5]=t[0];
x=z; p+=5;
}t=__num_table+x*5;
t[1]>'0'?p[1]=t[4],p[2]=t[3],p[3]=t[2],p[4]=t[1],p+=4,0:
t[2]>'0'?p[1]=t[4],p[2]=t[3],p[3]=t[2],p+=3,0:
t[3]>'0'?p[1]=t[4],p[2]=t[3],p+=2,0:
t[4]>'0'?*++p=t[4],0:0;
while(*s++=*p--); --s;
}
void _putll(char *&s,ll x){
if(x<0)*s++='-',x=-x;
if(x<=9){*s++=x+'0';return;}
char w[20],*p=w,*t; *p=0;
ll z;
while(x>9999){
z=x/100000; t=__num_table+(x-z*100000)*5;
p[1]=t[4]; p[2]=t[3]; p[3]=t[2]; p[4]=t[1]; p[5]=t[0];
x=z; p+=5;
}t=__num_table+x*5;
t[1]>'0'?p[1]=t[4],p[2]=t[3],p[3]=t[2],p[4]=t[1],p+=4,0:
t[2]>'0'?p[1]=t[4],p[2]=t[3],p[3]=t[2],p+=3,0:
t[3]>'0'?p[1]=t[4],p[2]=t[3],p+=2,0:
t[4]>'0'?*++p=t[4],0:0;
while(*s++=*p--); --s;
}
class FastInput{
public:
static const int MXL=83886080;
char *buf,*tt; int sz;
FastInput(int SZ=MXL){buf=(char*)malloc((sz=SZ)+1);tt=buf+1;*buf=*tt=0;}
~FastInput(){free(buf);}
void file_init(FILE *F){
buf[fread(buf+1,1,sz,F)]=0;
tt=buf+1;
}
void readint(int &x){_readint(tt,x);}
void readll(ll &x){_readll(tt,x);}
void readintmod(int &x,const int &ha){_readintmod(tt,x,ha);}
void readllmod(ll &x,const ll &ha){_readllmod(tt,x,ha);}
char getchar(){return *tt?*tt++:-1;}
void getchar(char &c){c=(*tt?*tt++:-1);}
void getstr(char *s){
char w;
while((w=*tt)&&w=='\r'||w=='\n'||w==' ')++tt;
while((w=*tt)&&w!='\r'&&w!='\n'&&w!=' ')*s++=w,++tt;
*s=0;
}
void getline(char *s){
char w;
while((w=*tt)&&w!='\r'&&w!='\n')*s++=w,++tt;
tt+=*tt=='\r'?1:0; tt+=*tt=='\n'?1:0; *s=0;
}
};
class FastOutput{
public:
static const int MXL=31457280;
char *buf,*tt; int sz;
FastOutput(int SZ=MXL){buf=(char*)malloc(sz=SZ);tt=buf;}
~FastOutput(){free(buf);}
void flush(FILE *F){
if(tt==buf)return;
fwrite(buf,1,tt-buf,F);
tt=buf;
}
void putint(const int &x){_putint(tt,x);}
void putll(const ll &x){_putll(tt,x);}
void putchar(const char &c){*tt++=c;}
void putstr(char *s){while(*tt++=*s++);--tt;}
};
}
#endif
const int N=105;
int debug,T,m,n,L[N][N],R[N][N],U[N][N],D[N][N],A[N][N];
vector<int>G[N*N];
zdw::FastInput I;
void ers(int x,int y){
A[x][y]=0;
int l,u,r,d;
for(l=y;l>=0&&!A[x][l];--l); ++l;
for(r=y;r<=m+1&&!A[x][r];++r); --r;
for(u=x;u>=0&&!A[u][y];--u); ++u;
for(d=x;d<=n+1&&!A[d][y];++d); --d;
for(int i=l;i<=r;++i)
L[x][i]=i-l,R[x][i]=r-i;
for(int i=u;i<=d;++i)
U[i][y]=i-u,D[i][y]=d-i;
if(r<m+1)L[x][r+1]=r-l+1;
if(l>0)R[x][l-1]=r-l+1;
if(u>0)D[u-1][y]=d-u+1;
if(d<n+1)U[d+1][y]=d-u+1;
}
bool cers(int x1,int y1,int x2,int y2){
for(int i=max(y1-L[x1][y1],y2-L[x2][y2]);i<=min(y1+R[x1][y1],y2+R[x2][y2]);++i){
if((x1<x2&&x1+D[x1][i]>=x2-1)||(x1>x2&&x1-U[x1][i]<=x2+1)||x1==x2)
return 1;
}
for(int i=max(x1-U[x1][y1],x2-U[x2][y2]);i<=min(x1+D[x1][y1],x2+D[x2][y2]);++i){
if((y1<y2&&y1+R[i][y1]>=y2-1)||(y1>y2&&y1-L[i][y1]<=y2+1)||y1==y2)
return 1;
}
return 0;
}
int main(){
I.file_init(stdin);
I.readint(T);
//cin>>T;
for(int P=1;P<=T;++P){
for(int i=0;i<N*N;++i)
G[i].clear();
memset(A,0,sizeof(L));
memset(L,0,sizeof(L));
memset(R,0,sizeof(L));
memset(U,0,sizeof(L));
memset(D,0,sizeof(L));
//cin>>n>>m;
I.readint(n);
I.readint(m);
map<int,int>M;
for(int i=1;i<=n;++i){
L[i][1]=1,R[i][m]=1;
U[i][0]=U[i][m+1]=i;
D[i][0]=D[i][m+1]=n+1-i;
}
for(int i=1;i<=m;++i){
U[1][i]=1,D[n][i]=1;
L[0][i]=L[n+1][i]=i;
R[0][i]=R[n+1][i]=m+1-i;
}
int p=0;
for(int i=1;i<=n;++i){
for(int j=1;j<=m;++j){
//cin>>A[i][j];
I.readint(A[i][j]);
G[A[i][j]].push_back(i<<8|j);
}
}
int cnt=n*m>>1;
while(cnt){
int flg=0;
for(int i=0;i<N*N;++i){
if(!G[i].size())continue;
if(G[i].size()&1){
flg=0;
break;
}
assert(G[i].size()==2);
if(cers(G[i][0]>>8,G[i][0]&0xff,G[i][1]>>8,G[i][1]&0xff))
ers(G[i][0]>>8,G[i][0]&0xff),
ers(G[i][1]>>8,G[i][1]&0xff),
flg=1,--cnt,G[i].clear();
}
if(!flg)break;
}
cout<<(cnt?"No":"Yes")<<endl;
}
}