为什么没有summer day 6? 因为昨天是周日。
今天上午训练赛。后面题目太长没看,ABCE题觉得可以做。
A. Uva 10082
简单字符串处理题。我之前还智障地想用map,结果不用考虑QAZ这三个字符,果断用数组一次A了。
#include <cstdio>
#include <cstdlib>
const char s[] = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";
char getCorrectChar(char c)
{
int i;
for (i = 1; i < sizeof(s); ++ i)
{
if (s[i] == c)
return s[i-1];
}
return c;
}
int main(void)
{
char c;
while ((c = getchar()) != EOF)
putchar(getCorrectChar(c));
return 0;
}
(代码不是我写的)
B. Uva 524
给出长度,生成按照字典序排列列的素数环。
Sample Input
6
8
Sample Output
Case 1:
1 4 3 2 5 6
1 6 5 2 3 4
Case 2:
1 2 3 8 5 6 7 4
1 2 5 8 3 4 7 6
1 4 7 6 5 8 3 2
1 6 7 4 3 8 5 2
DFS,PE又是一个悬念。
#include<cstdio>
#include<cstring>
int isPrime[33] = {0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0};
//0, 1, 2. 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
int isVis[18];
int n;
int ring[18];
void primering(int len, int pos)
{
if(pos>=len)
return;
for(int i = 2; i<=len; i++)
{
if(isVis[i] == 1)
continue;
if(isPrime[i+ring[pos]])
{
ring[pos+1] = i;
isVis[i] = 1;
if(pos == len-1 && isPrime[i+1])
{
for(int k = 1; k<len; k++)
printf("%d ",ring[k]);
printf("%d\n",ring[pos+1]);
isVis[i] = 0;
continue;
}
else
primering(len, pos+1);
isVis[i] = 0;
}
}
return;
}
int main()
{
int kase = 1;
while(scanf("%d",&n) == 1)
{
memset(isVis, 0, sizeof(isVis));
memset(ring, 0, sizeof(ring));
ring[1] = 1; isVis[1] = 1;
printf("Case %d:\n",kase++);
primering(n, 1);
printf("\n");
}
return 0;
}
C. Uva 297
给出四分树先序排列,求两个图像叠加之和黑色部分面积。
总之就是递归读取再维护状态,我抄书的。= =
#include<cstdio>
#include<cstring>
const int len = 32;
const int maxn = 1024+10;
char s[maxn];
int buf[len][len], cnt;
void draw(const char* s, int& p, int r, int c, int w)
{
char ch = s[p++];
if(ch == 'p')
{
draw(s, p, r, c+w/2, w/2);
draw(s, p, r, c, w/2);
draw(s, p, r+w/2, c, w/2);
draw(s, p, r+w/2, c+w/2, w/2);
}
else if(ch == 'f')
{
for(int i = r; i< r+w; i++)
{
for(int j = c; j<c+w; j++)
if(buf[i][j] == 0)
{
buf[i][j] == 1; cnt++;
}
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(buf, 0, sizeof(buf));
cnt = 0;
for(int i = 0; i<2; i++){
scanf("%s", s);
int p = 0;
draw(s, p, 0, 0, len);
}
}
printf("There are %d black pixels.\n", cnt);
return 0;
}
D. Uva 298
没看,贴出大神BFS代码。、
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
using namespace std;
const int N=35;
int f[N][N][11][11],a[N][N],T,n,m,Q,sx,sy,ex,ey,i,j,k,lx,ly,rx,ry,b[N*N*360],vx[N*N*360],vy[N*N*360];
inline int Z(int a) { return (a<0)?-a:a; }
inline void bfs()
{ int l,r,i,j,X,Y,VX,VY,dx,dy,x,y;
memset(f,0,sizeof(f));
f[sx][sy][3][3]=1;
b[l=r=1]=sx*m+sy; vx[l]=vy[l]=0;
while (l<=r)
{ X=b[l]/m,Y=b[l]%m,VX=vx[l],VY=vy[l];
if ((X==ex)&&(Y==ey)) {
printf("Optimal solution takes %d hops.\n",f[ex][ey][VX+3][VY+3]-1);
return;
}
for (i=-1;i<=1;i++) for (j=-1;j<=1;j++)
{ dx=VX+i,dy=VY+j;
if ((Z(dx)>3)||(Z(dy)>3)) continue;
x=X+dx,y=Y+dy;
if ((x<0)||(x>=n)||(y<0)||(y>=m)) continue;
if (a[x][y]||f[x][y][dx+3][dy+3]) continue;
f[x][y][dx+3][dy+3]=f[X][Y][VX+3][VY+3]+1;
b[++r]=x*m+y,vx[r]=dx,vy[r]=dy;
} l++;
}
printf("No solution.\n");
}
int main()
{ freopen("D.in","r",stdin);
freopen("D.out","w",stdout);
for (scanf("%d",&T);T;T--)
{ scanf("%d%d",&n,&m);
scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
scanf("%d",&Q); memset(a,0,sizeof(a));
for (i=1;i<=Q;i++)
{ scanf("%d%d%d%d",&lx,&rx,&ly,&ry);
for (j=lx;j<=rx;j++)
for (k=ly;k<=ry;k++) a[j][k]=1;
}
bfs();
}
return 0;
}
E. Uva 299
其实是冒泡排序······