这段代码会在#10 TLE
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
class pos {
public:
pos() {
x=y=0;
return;
}
pos(int a,int b) {
x=a,y=b;
return;
}
bool operator==(const pos &tar) {
return (x==tar.x)&&(y==tar.y);
}
int x,y;
};
int n,m;
class image {
public:
image() {
memset(this,0,sizeof(image));
return;
}
image(const image &tar) {
memcpy(this,&tar,sizeof(image));
return;
}
bool operator>(const image &tar) {
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
if(arr[i][j] && (!tar.arr[i][j]))
return true;
// printf("unaccecpt!\n");
return false;
}
bool operator==(const image &tar) {
return !memcmp(this,&tar,sizeof(image));
}
bool write(const pos &p);
void read() {
///////////1:
char buf[1024],cmd[16];
sprintf(cmd,"%%%ds",m);
for(int i=0;i<n;i++){
scanf(cmd,buf);
for(int j=0;j<m;j++)
arr[i][j]=(buf[j]=='#')?true:false;
}
//////////2:
int px=0,py=0;
for(; px<n; px++)
for(py=0; py<m;) {
char ch=getchar();
if(ch=='#') {
arr[px][py]=true;
py++;
}
else if(ch=='.') {
arr[px][py]=false;
py++;
}
}
return;
}
bool arr[1024][1024];
} start;
bool image::write(const pos &p) {
if(p.x<1 || p.x>n-2 || p.y<1 || p.y>m-2)
return false;
if( start.arr[p.x+1][p.y ] &&
start.arr[p.x-1][p.y ] &&
start.arr[p.x ][p.y+1] &&
start.arr[p.x ][p.y-1] &&
start.arr[p.x+1][p.y+1] &&
start.arr[p.x+1][p.y-1] &&
start.arr[p.x-1][p.y+1] &&
start.arr[p.x-1][p.y-1] ) {
arr[p.x+1][p.y ]=true;
arr[p.x-1][p.y ]=true;
arr[p.x ][p.y+1]=true;
arr[p.x ][p.y-1]=true;
arr[p.x+1][p.y+1]=true;
arr[p.x+1][p.y-1]=true;
arr[p.x-1][p.y+1]=true;
arr[p.x-1][p.y-1]=true;
return true;
}
return false;
}
queue<image> q;
bool bfs() {
q.push(image());
while(!q.empty()) {
image now=q.front();
q.pop();
for(int i=0; i<n; i++)
for(int j=0; j<m; j++) {
image dst=now;
if(!dst.write(pos(i,j)))
continue;
if(dst==now || dst>start)
continue;
q.push(dst);
if(dst==start) {
while(!q.empty())
q.pop();
return true;
}
}
}
return false;
}
int main() {
scanf("%d%d",&n,&m);
start.read();
if(image()==start)
puts("YES");
else
puts(bfs()?"YES":"NO");
return 0;
}
去Codeforces一看,#10的输入是这个:
1000 1000
.......................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
令人震惊的是,虽然它应该为1000*1000的矩阵,下面的“.”却远远不够!不知道是CF显示不全还是什么。题解里的AC代码都可以读入这个样例,为什么我的不可以?!?!?!