C++11——【USACO 4.4.3】——Frame Up
2017-10-20 07:48
351 查看
Frame Up
Consider the following five picture frames shown on an 9 x 8 array:
Now place all five picture frames on top of one another starting with 1 at the bottom and ending up with 5 on top. If any part of a frame covers another frame, it hides that part of the frame below.
Viewing the stack of five frames we see the following.
Given a picture like this, determine the order of the frames stacked from bottom to top.
Here are the rules for this challenge:
The width of the frame is always exactly 1 character and the sides are never shorter than 3 characters.
It is possible to see at least one part of each of the four sides of a frame. A corner is part of two sides.
The frames will be lettered with capital letters, and no two frames will be assigned the same letter.
lines. There will always be at least one legal ordering.
装框
考虑下面的5个图框,在9 x 8数组:
现在把这五张图片放在另一个上面,从底部开始,最后在上面画上5个。如果框架的任何部分覆盖了另一个框架,它就会隐藏下面这个框架的一部分。查看重叠的框架,我们看到了下面的内容。
给定这样的图片,确定从底部到顶部的顺序。
以下是这一挑战的规则:
框架的宽度始终是1个字符,而边的长度从不小于3个字符。
在一个框架的四边中,至少可以看到一个部分。一个角落是双方的一部分。
框架将用大写字母来表示,没有两个框架会被分配到相同的字母。
项目名称:frameup
输入格式
第1行:两个空格分隔的整数:高度 H (3<= H <=30)和宽度W(3 <= W <= 30)。
2号线. .H+1:H行,每有W字。
示例输入(文件frameup.in)
输出格式
将字母按照顺序从下到上排列。如果一种排序有多种可能,那么按字母顺序排列所有这些可能性——按字母顺序排列。总会有至少一种合法的答案。
样例输出(文件frameup.out)
EDABC
/*
ID : mcdonne1
LANG : C++11
TASK : frameup
*/
#pragma GCC optimize("O3")
#include <iostream>
#include <fstream>
#include <cstring>
#include <algorithm>
#include <set>
using namespace std;
struct node{
int maxx, maxy, minx = 1e5, miny = 1e5;
}a[26];
int n, m, num;
int way[30];
bool went[26];
char c[35][35];
set <string> ans;
void dfs (int step, char p[35][35]) {
char __p[35][35];
if (step > num) {
string __ans = "";
for (int i = num; i >= 1; --i) __ans += way[i] + 'A';
ans.insert(__ans);
return;
}
for(int k = 0; k < 26; k++)
if(went[k]) {
for (int i = 0; i <= 30; i++)
for (int j = 0; j <= 30; j++)
__p[i][j] = p[i][j];
int _x = a[k].maxy;
int _y = a[k].miny;
for (int i = a[k].minx; i <= a[k].maxx; i++)
{
if ((p[_x][i] != 0 and p[_x][i] != k + 'A') or (p[_y][i] != 0 and p[_y][i] != k + 'A')) goto Break;
__p[_x][i] = 0;
__p[_y][i] = 0;
}
_x = a[k].maxx;
_y = a[k].minx;
for (int i = a[k].miny; i <= a[k].maxy; i++)
{
if ((p[i][_y] != 0 and p[i][_y] != k + 'A') or (p[i][_x] != 0 and p[i][_x] != k + 'A')) goto Break;
__p[i][_x] = 0;
__p[i][_y] = 0;
}
went[way[step] = k] = false;
dfs (step + 1, __p);
went[k] = true;
Break : ;
}
}
int main () {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ifstream fin ("frameup.in", ios::in);
ofstream fout ("frameup.out", ios::out);
fin>>n>>m;
for (int i = 1; i <= n; i++) {
fin>>c[i] + 1;
for (int j = 1; j <= m; j++) {
if(c[i][j] == '.') continue;
int x = c[i][j] - 'A';
a[x].minx = min(a[x].minx, j);
a[x].maxx = max(a[x].maxx, j);
a[x].miny = min(a[x].miny, i);
a[x].maxy = max(a[x].maxy, i);
went[x] = true;
}
}
for (int i = 0; i < 26; i++)
if(went[i])
++num;
dfs(1, c);
for (set <string> :: iterator it = ans.begin(); it != ans.end(); it++)
fout<<*it<<endl;
return 0;
}
Consider the following five picture frames shown on an 9 x 8 array:
........ ........ ........ ........ .CCC.... EEEEEE.. ........ ........ ..BBBB.. .C.C.... E....E.. DDDDDD.. ........ ..B..B.. .C.C.... E....E.. D....D.. ........ ..B..B.. .CCC.... E....E.. D....D.. ....AAAA ..B..B.. ........ E....E.. D....D.. ....A..A ..BBBB.. ........ E....E.. DDDDDD.. ....A..A ........ ........ E....E.. ........ ....AAAA ........ ........ EEEEEE.. ........ ........ ........ ........ 1 2 3 4 5
Now place all five picture frames on top of one another starting with 1 at the bottom and ending up with 5 on top. If any part of a frame covers another frame, it hides that part of the frame below.
Viewing the stack of five frames we see the following.
.CCC... ECBCBB.. DCBCDB.. DCCC.B.. D.B.ABAA D.BBBB.A DDDDAD.A E...AAAA EEEEEE..
Given a picture like this, determine the order of the frames stacked from bottom to top.
Here are the rules for this challenge:
The width of the frame is always exactly 1 character and the sides are never shorter than 3 characters.
It is possible to see at least one part of each of the four sides of a frame. A corner is part of two sides.
The frames will be lettered with capital letters, and no two frames will be assigned the same letter.
PROGRAM NAME: frameup
INPUT FORMAT
Line 1: | Two space-separated integers: the height H (3 <= H <=30) and the width W (3 <= W <= 30). |
Line 2..H+1: | H lines, each with a string W characters wide. |
SAMPLE INPUT (file frameup.in)
9 8 .CCC.... ECBCBB.. DCBCDB.. DCCC.B.. D.B.ABAA D.BBBB.A DDDDAD.A E...AAAA EEEEEE..
OUTPUT FORMAT
Print the letters of the frames in the order they were stacked from bottom to top. If there are multiple possibilities for an ordering, list all such possibilities -- in alphabetical order -- on successivelines. There will always be at least one legal ordering.
SAMPLE OUTPUT (file frameup.out)
EDABC
装框
考虑下面的5个图框,在9 x 8数组:
........ ........ ........ ........ .CCC.... EEEEEE.. ........ ........ ..BBBB.. .C.C.... E....E.. DDDDDD.. ........ ..B..B.. .C.C.... E....E.. D....D.. ........ ..B..B.. .CCC.... E....E.. D....D.. ....AAAA ..B..B.. ........ E....E.. D....D.. ....A..A ..BBBB.. ........ E....E.. DDDDDD.. ....A..A ........ ........ E....E.. ........ ....AAAA ........ ........ EEEEEE.. ........ ........ ........ ........ 1 2 3 4 5
现在把这五张图片放在另一个上面,从底部开始,最后在上面画上5个。如果框架的任何部分覆盖了另一个框架,它就会隐藏下面这个框架的一部分。查看重叠的框架,我们看到了下面的内容。
.CCC... ECBCBB.. DCBCDB.. DCCC.B.. D.B.ABAA D.BBBB.A DDDDAD.A E...AAAA EEEEEE..
给定这样的图片,确定从底部到顶部的顺序。
以下是这一挑战的规则:
框架的宽度始终是1个字符,而边的长度从不小于3个字符。
在一个框架的四边中,至少可以看到一个部分。一个角落是双方的一部分。
框架将用大写字母来表示,没有两个框架会被分配到相同的字母。
项目名称:frameup
输入格式
第1行:两个空格分隔的整数:高度 H (3<= H <=30)和宽度W(3 <= W <= 30)。
2号线. .H+1:H行,每有W字。
示例输入(文件frameup.in)
9 8 .CCC.... ECBCBB.. DCBCDB.. DCCC.B.. D.B.ABAA D.BBBB.A DDDDAD.A E...AAAA EEEEEE..
输出格式
将字母按照顺序从下到上排列。如果一种排序有多种可能,那么按字母顺序排列所有这些可能性——按字母顺序排列。总会有至少一种合法的答案。
样例输出(文件frameup.out)
EDABC
/*
ID : mcdonne1
LANG : C++11
TASK : frameup
*/
#pragma GCC optimize("O3")
#include <iostream>
#include <fstream>
#include <cstring>
#include <algorithm>
#include <set>
using namespace std;
struct node{
int maxx, maxy, minx = 1e5, miny = 1e5;
}a[26];
int n, m, num;
int way[30];
bool went[26];
char c[35][35];
set <string> ans;
void dfs (int step, char p[35][35]) {
char __p[35][35];
if (step > num) {
string __ans = "";
for (int i = num; i >= 1; --i) __ans += way[i] + 'A';
ans.insert(__ans);
return;
}
for(int k = 0; k < 26; k++)
if(went[k]) {
for (int i = 0; i <= 30; i++)
for (int j = 0; j <= 30; j++)
__p[i][j] = p[i][j];
int _x = a[k].maxy;
int _y = a[k].miny;
for (int i = a[k].minx; i <= a[k].maxx; i++)
{
if ((p[_x][i] != 0 and p[_x][i] != k + 'A') or (p[_y][i] != 0 and p[_y][i] != k + 'A')) goto Break;
__p[_x][i] = 0;
__p[_y][i] = 0;
}
_x = a[k].maxx;
_y = a[k].minx;
for (int i = a[k].miny; i <= a[k].maxy; i++)
{
if ((p[i][_y] != 0 and p[i][_y] != k + 'A') or (p[i][_x] != 0 and p[i][_x] != k + 'A')) goto Break;
__p[i][_x] = 0;
__p[i][_y] = 0;
}
went[way[step] = k] = false;
dfs (step + 1, __p);
went[k] = true;
Break : ;
}
}
int main () {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ifstream fin ("frameup.in", ios::in);
ofstream fout ("frameup.out", ios::out);
fin>>n>>m;
for (int i = 1; i <= n; i++) {
fin>>c[i] + 1;
for (int j = 1; j <= m; j++) {
if(c[i][j] == '.') continue;
int x = c[i][j] - 'A';
a[x].minx = min(a[x].minx, j);
a[x].maxx = max(a[x].maxx, j);
a[x].miny = min(a[x].miny, i);
a[x].maxy = max(a[x].maxy, i);
went[x] = true;
}
}
for (int i = 0; i < 26; i++)
if(went[i])
++num;
dfs(1, c);
for (set <string> :: iterator it = ans.begin(); it != ans.end(); it++)
fout<<*it<<endl;
return 0;
}
相关文章推荐
- usaco 4.4.3 Frame Up
- 【USACO题库】4.4.3 Frame Up重叠的图像
- USACO 4.4.3 Frame Up 重叠的图像 题解与分析
- USACO4.4.3 Frame Up (frameup)
- USACO4.4.3 Frame Up (frameup)
- usaco 4.4.3 Frame Up
- USACO 4.4 Frame Up
- usaco 4.4.3
- 【USACO4.4.3】重叠的图像(DAG图的拓扑排序)
- USACO 4.4 Frame Up (拓扑排序)
- Frame Up[USACO]
- USACO 4.4 Frame Up(拓扑排序)
- usaco training 4.4.3 Frame Up 题解
- usaco 4.4 Frame Up 2010.8.8
- usaco 4.4 Frame Up(拓扑排序)
- Frame Up_usaco 4.4_拓扑排序
- bzoj1573 [Usaco2009 Open]牛绣花cowemb
- bzoj 1593: [Usaco2008 Feb]Hotel 旅馆 线段树
- Usaco 1.2.4 回文平方数(Palindromic Squares)
- usaco-milk2-pass