2015-12-03 画图

在这里插入图片描述

BFS深搜+hashtable来判断是横线还是竖线
但是为啥还是90分啊呜呜!找不到原因

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<map>
using namespace std;
int n, q, m;
int op;//操作
const int maxn = 110;
char pic[maxn][maxn];//画布
int hashtable[maxn][maxn];//=0表示没有画线//==1表示画横线,=2表示画竖线=3表示画十字
int inq[maxn][maxn];
int X[4] = {
    
     0,0,1,-1 };
int Y[4] = {
    
     1,-1,0,0 };
struct node {
    
    
	int x;
	int y;
}Node;
int judge(int x, int y)//检测(x,y)是否合法
{
    
    
	if (x >= m || x < 0 || y >= n || y < 0) return 0;//越界
	if (hashtable[y][x] != 0 || inq[y][x] == 1) return 0;//已经被画线了或者已经入过队了
	return 1;
}
void BFS(int x,int y,char c)
{
    
    
	queue<node> Q;
	Node.x = x; Node.y = y;
	Q.push(Node);
	inq[y][x] = 1;//记录为入队
	pic[y][x] = c;
	while (!Q.empty())
	{
    
    
		node top = Q.front();
		Q.pop();
		for (int i = 0; i < 4; i++)
		{
    
    
			int newX = top.x + X[i];
			int newY = top.y + Y[i];
			if (judge(newX, newY))
			{
    
    
				Node.x = newX;
				Node.y = newY;
				Q.push(Node);
				inq[newY][newX] = 1;
				pic[newY][newX] = c;
			}
		}
	}
}
int main() {
    
    
	scanf("%d %d %d",&m,&n,&q);//一共m列n行
	getchar();
	//初始化画布:
	for (int y = 0; y < n; y++)
	{
    
    
		for (int x = 0; x < m; x++)
		{
    
    
			pic[y][x] = '.';
		}
	}
	//初始化hashtable
	fill(hashtable[0],hashtable[0]+maxn*maxn,0);
	for (int i = 0; i < q; i++)
	{
    
    
		scanf("%d",&op);
		if (op==1)//填充操作
		{
    
    
			//使用BFS进行广度优先搜索填充
			//每次填充字符的时候都要初始化inq
			fill(inq[0],inq[0]+maxn*maxn,0);
			int x, y;
			char c[3];
			scanf("%d %d %s", &x, &y, c);
			BFS(x,y,c[0]);

		}
		else//画线段操作
		{
    
    
			int x1, y1, x2, y2;
			scanf("%d %d %d %d",&x1,&y1,&x2,&y2);			
			if (x1 == x2)//表示画竖线
			{
    
    
				if (y1 > y2)
					swap(y1,y2);
				for (int y = y1; y <= y2; y++)
				{
    
    
					if(hashtable[y][x1]==0)
						hashtable[y][x1] = 2;
					else
						hashtable[y][x1] = 3;
				}
			}
			else//表示画横线
			{
    
    
				if (x1 > x2)
					swap(x1,x2);
				for (int x = x1; x <= x2; x++)
				{
    
    
					if (hashtable[y1][x] == 0)
						hashtable[y1][x] = 1;
					else
						hashtable[y1][x] = 3;
				}
			}
			
				
		}
	}
	//现在把画线段的操作放进pic中
	for (int y = 0; y < n; y++)
	{
    
    
		for (int x = 0; x < m; x++)
		{
    
    
			if (hashtable[y][x] == 1)//说明画横线

				pic[y][x] = '-';
			else if (hashtable[y][x] == 2)
				pic[y][x] = '|';//画竖线
			else if (hashtable[y][x] == 3)//画十字架
				pic[y][x] = '+';
			else
				;
		}
	}
	for (int y = n-1; y>=0;y--)
	{
    
    
		for (int x = 0; x < m; x++)
		{
    
    
			printf("%c", pic[y][x]);
		}
		printf("\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/susuate/article/details/120120016
今日推荐