⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 2645729_wa.cpp

📁 北大大牛代码 1240道题的原代码 超级权威
💻 CPP
字号:
#include <stdio.h>
#include <ctype.h>
#include <queue>
#include <vector>
#include <algorithm>

using namespace std;

char map[1001][1001];

int W, H;

struct node 
{
	char name[20];
	char map[101][101];
	int w, h;
}pic[101];

int n, m;

struct Node 
{
	int no;
	char id[101];
	int x, y;
	bool pos;
	int layer;
	char file[101], rel[101];
}cs[501];

bool cmp(struct Node a,struct Node b)
{
	if (a.layer==b.layer)
	{
		return a.no < b.no;
	} 
	else
	{
		return a.layer < b.layer;
	}
}

int get_num()
{
	bool mark;
	char ch;
	int l = 1;
	char num[10];

	mark = 0;
	while ((ch=getchar())!='\n')
	{
		if(mark)
		{
			if (ch=='-'||isdigit(ch))
			{
				num[0] = ch;
				while ((ch=getchar())&&isdigit(ch))
				{
					num[l++] = ch;
				}
				num[l] = '\0';mark = 0;
			}
		}
		else
			if (ch=='-')
			{
				mark = 1;
			}
	}
	return atoi(num);
}

void input()
{
	int i;

	scanf("%d",&n);
	for (i = 0; i < n; i++)
	{
		scanf("%s%d%d",pic[i].name,&pic[i].h,&pic[i].w);
		for (int j = 0; j < pic[i].h; j++)
		{
			scanf("%s",pic[i].map[j]);
		}
	}
	scanf("%d",&m);
	getchar();
	char ch, tmp[101], l;

	for (i = 0; i < m; i++)
	{
		cs[i].no = i;
		while ((ch=getchar())!='\n')
		{
			if (ch=='#')
			{
				l = 0;
				while ((ch=getchar())&&ch!=' '&&ch!='{')
				{
					tmp[l++] = ch;
				}
				tmp[l] = '\0';
			}
		}
		strcpy(cs[i].id,tmp);
		cs[i].x = get_num();
		cs[i].y = get_num();
		while ((ch=getchar())!='\n')
		{
			if (ch=='a')
			{
				cs[i].pos = 0;
				gets(tmp);
				break;
			}
			if (ch=='r')
			{
				cs[i].pos = 1;
				while (ch=getchar())
				{
					if (ch=='=')
					{
						scanf("%s",tmp);
						strcpy(cs[i].rel,tmp);
						if (cs[i].rel[strlen(cs[i].rel)-1]==';')
						{
							cs[i].rel[strlen(cs[i].rel)-1] = '\0';
						}
						gets(tmp);
						goto end;
					}
				}
			}
		}
end:
		;
		while (ch=getchar())
		{
			if (ch==':')
			{
				scanf("%s",cs[i].file);
				if (cs[i].file[strlen(cs[i].file)-1]==';')
				{
					cs[i].file[strlen(cs[i].file)-1] = '\0';
				}
				gets(tmp);
				break;
			}
		}
		while (ch=getchar())
		{
			if (isdigit(ch))
			{
				tmp[0] = ch;
				scanf("%s",&tmp[1]);
				cs[i].layer = atoi(tmp);
				gets(tmp);
				break;
			}
		}
		gets(tmp);
	}
}

void output()
{
	int i;

	for (i = 0; i < m; i++)
	{
		printf("#%s {\n",cs[i].id);
		printf("	pos-x: %d px;\n",cs[i].x);
		printf("	pos-y: %d px;\n",cs[i].y);
		printf("	position: ");
		if (cs[i].pos)
		{
			printf("relative = %s",cs[i].rel);
		}
		else
		{
			printf("absolute");
		}
		puts(";");
		printf("	file: %s;\n",cs[i].file);
		printf("	layer: %d;\n",cs[i].layer);
		printf("}\n");
	}
}

void solve()
{
	int i, j;
	vector <int> tree[501];
	queue <int> que;

	for (i = 0; i < m; i++)
	{
		if (cs[i].pos)
		{
			for (j = 0; j < m; j++)
			{
				if (strcmp(cs[i].rel,cs[j].id)==0)
				{
					tree[j].push_back(i);
					break;
				}
			}
		}
		else
			que.push(i);
	}
	while (!que.empty())
	{
		i = que.front();
		que.pop();
		for (j = 0; j < tree[i].size(); j++)
		{
			cs[tree[i][j]].x += cs[i].x;
			cs[tree[i][j]].y += cs[i].y;
			if (tree[tree[i][j]].size())
			{
				que.push(tree[i][j]);
			}
		}
	}
}

int main()
{
	int i, t, cas;
	int l, k, p;
	int ii, jj;

	cas = 1;
	scanf("%d",&t);
	while (t--)
	{
		input();
		sort(cs,cs+m,cmp);
		W = H = 0;
		memset(map,'.',sizeof(map));
		solve();
		//output();
		//printf("\n");
		//system("pause");
		for (i = 0; i < m; i++)
		{
			for (l = 0; l < n; l++)
			{
				if (strcmp(pic[l].name,cs[i].file)==0)
				{
					p = l;
					break;
				}
			}
			ii = cs[i].y;jj = cs[i].x;
			if (ii+pic[p].h>H)
			{
				H = ii+pic[p].h;
			}
			if (jj+pic[p].w>W)
			{
				W = jj+pic[p].w;
			}
			for (l = ii; l < ii+pic[p].h; l++)
			{
				for (k = jj; k < jj+pic[p].w; k++)
				{
					map[l][k] = pic[p].map[l-ii][k-jj];
				}
			}
		}
		printf("Scenario #%d:\n",cas++);
		for (l = 0; l < H; l++)
		{
			for (k = 0; k < W; k++)
			{
				if (map[l][k]=='.')
				{
					printf(" ");
				}
				else
				{
					printf("%c",map[l][k]);
				}	
			}
			printf("\n");
		}
		printf("\n");
	}
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -