BFS,这道题比较讨厌的地方是通常情况下一个二维数组的起点在左上角,而这道题的起点在左下角,那么在处理的过程中就需要对纵坐标也就是数组的第一维度进行特殊处理。
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cstring>
#include <vector>
#include <sstream>
#include <queue>
using namespace std;
char a[110][110];
struct node{
int x, y;
char c;
node(int xx, int yy, char cc)
{
x = xx;
y = yy;
c = cc;
}
};
int X[4] = {0, 0, 1, -1};
int Y[4] = {1, -1, 0, 0};
int vis[110][110];
int m, n, q;
void BFS(int x, int y, char c)
{
memset(vis, false, sizeof(vis));
queue<node> q;
q.push(node(x, n - y - 1, c));
while(!q.empty())
{
node f = q.front();
q.pop();
for(int i = 0 ; i < 4 ; ++i)
{
int newX = f.x + X[i];
int newY = f.y + Y[i];
if(newX >= 0 && newX < m && n - newY - 1 >= 0 && n - newY - 1 < n && a[n - newY - 1][newX] != '|' && a[n - newY - 1][newX] != '-' && a[n - newY - 1][newX] != '+' && !vis[n - newY - 1][newX])
{
a[n - newY - 1][newX] = c;
q.push(node(newX, newY, c));
vis[n - newY - 1][newX] = true;
}
}
}
}
int main()
{
cin>>m>>n>>q;
for(int i = n - 1 ; i >= 0 ; --i)
{
for(int j = 0 ; j < m ; ++j)
a[i][j] = '.';
}
int type, x1, x2, y1, y2, x, y;
char c;
for(int i = 0 ; i < q ; ++i)
{
cin>>type;
if(type == 0)
{
cin>>x1>>y1>>x2>>y2;
if(x1 == x2)
{
if(y1 > y2)
swap(y1, y2);
for(int j = y1 ; j <= y2 ; ++j)
{
if(a[n - j - 1][x1] == '-')
a[n - j - 1][x1] = '+';
else if(a[n - j - 1][x1] != '+')
a[n - j - 1][x1] = '|';
}
}
else if(y1 == y2)
{
y1 = n - y1 - 1;
if(x1 > x2)
swap(x1, x2);
for(int j = x1 ; j <= x2 ; ++j)
{
if(a[y1][j] == '|')
a[y1][j] = '+';
else if(a[y1][j] != '+')
{
a[y1][j] = '-';
}
}
}
}
else if(type == 1)
{
cin>>x>>y>>c;
y = n - y - 1;
BFS(x, y, c);
}
}
for(int i = 0 ; i < n ; ++i)
{
for(int j = 0 ; j < m ; ++j)
{
cout<<a[i][j];
}
cout<<endl;
}
}