Problem L. Visual Cube
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1473 Accepted Submission(s): 694
Problem Description
Little Q likes solving math problems very much. Unluckily, however, he does not have good spatial ability. Everytime he meets a 3D geometry problem, he will struggle to draw a picture.
Now he meets a 3D geometry problem again. This time, he doesn't want to struggle any more. As a result, he turns to you for help.
Given a cube with length a, width b and height c, please write a program to display the cube.
Input
The first line of the input contains an integer T(1≤T≤50), denoting the number of test cases.
In each test case, there are 3 integers a,b,c(1≤a,b,c≤20), denoting the size of the cube.
Output
For each test case, print several lines to display the cube. See the sample output for details.
Sample Input
2
1 1 1
6 2 4
Sample Output
解题思路:立体图分为:俯视图 + 正视图 + 右视图,此题先刷 俯视图 + 正视图,最后刷 右视图;一开始先记录每个视图有哪些规律图案,比如:“+-+-+-+”、“/././././”,然后再创建一个全是“点”的二维数组,把它描上去即可。
AC 代码
using namespace std;
typedef long long ll;
int main()
{
int T; scanf("%d",&T);
int a,b,c;
while(T-- && ~scanf("%d%d%d",&a,&b,&c))
{
string zf,xd,sd,dx,sz;
for(int i=0;i<a;i++)
{
zf.append("+-");
xd.append("/.");
sd.append("|.");
}
zf.append("+"); xd.append("/"); sd.append("|");
for(int i=0;i<c;i++)
{
sz.append("|+");
dx.append("./");
}
// cout<<"+-:"<<zf<<endl;
// cout<<"/.:"<<xd<<endl;
// cout<<"|.:"<<sd<<endl;
// cout<<"./:"<<dx<<endl;
// cout<<"|+:"<<sz<<endl;
int rlen=2*c+1+2*b,clen=2*a+1+2*b;
int con=2*b;
char g[rlen+5][clen+5]; mem(g,'.');
for(int i=0;i<rlen;i++,con--)
{
for(int j=0;j<clen;j++)
{
if(con>0)
{
if(j+con<clen && i%2==0 && j<zf.length())
g[i][j+con]=zf[j];
else if(j+con<clen && i%2==1 && j<xd.length())
g[i][j+con]=xd[j];
}
else
{
if(i%2==0 && j<zf.length())
g[i][j]=zf[j];
else if(i%2==1 && j<sd.length())
g[i][j]=sd[j];
}
}
}
// 正视图和俯视图刷完,可以快速排错
// for(int i=0;i<rlen;i++)
// {
// for(int j=0;j<clen;j++)
// {
// printf("%c",g[i][j]);
// }
// puts("");
// }
// puts("-----------------");
// 右视图刷完
con=2*b;
int h=2*c,t=0;
for(int j=clen-1;j>=0 && con--;j--,t++)
{
for(int i=1+t;i<=h+t;i++)
{
if(j%2==0 && i-1-t<sz.length())
g[i][j]=sz[i-1-t];
else if(j%2==1 && i-1-t<dx.length())
g[i][j]=dx[i-1-t];
}
}
for(int i=0;i<rlen;i++)
{
for(int j=0;j<clen;j++)
{
printf("%c",g[i][j]);
}
puts("");
}
}
return 0;
}