P2960 写了 100 行代码来完成 30 行就能完成的事,是我不适合 OOP 吗?
#include <bits/stdc++.h>
using namespace std;
class slove{
static const int N = 105;
int n,m;
int mx,my;
char maps[N][N];
int vis[N][N];
int cnt;
const int dir[8][2] =
{
{-1,-1},
{-1,0},
{-1,1},
{0,1},
{0,-1},
{1,0},
{1,1},
{1,-1}
};
public:
class nodes_of_bfs
{
public:
int x,y,step;
nodes_of_bfs(int xx = 0,int yy = 0,int stepp = 0):x(xx),y(yy),step(stepp){}
};
void input(){
scanf("%d%d",&m,&n);
scanf("%d%d",&mx,&my);
int temp = mx;
mx = n - my + 1;
my = temp;
cnt = 0;
memset(maps,'*',sizeof(maps));
memset(vis,0,sizeof(vis));
for(int i = 1;i <= n;i ++)
{
scanf("%s",maps[i]);
for(int j = m-1;j >= 0;j --){
maps[i][j+1] = maps[i][j];
}
maps[i][0] = '*';
}
for(int i = 1;i <= n;i ++)
{
for(int j = 1;j <= m;j ++){
if(maps[i][j] == '.')
{
cnt ++;
}
}
}
}
int bfs(){
nodes_of_bfs start(mx,my);
queue<nodes_of_bfs> que;
que.push(start);
vis[mx][my] = 1;
cnt --;
while(!que.empty())
{
nodes_of_bfs now = que.front();
que.pop();
for(int i = 0;i < 8;i ++)
{
nodes_of_bfs next = now;
next.x += dir[i][0];
next.y += dir[i][1];
next.step ++;
if(maps[next.x][next.y] == '.')
{
if(vis[next.x][next.y] == 0)
{
vis[next.x][next.y] = 1;
que.push(next);
cnt --;
}
if(cnt == 0)
{
return next.step;
}
}
}
}
return 0;
}
slove()
{
input();
printf("%d\n",bfs());
}
~slove()
{
exit(0);
}
};
int main() {
slove slover;
return 0;
}