发现,其实题意都是挺好懂的,如果自己在本子上画画,感觉还是挺简单的,可是转移到代码的时候,发现好难,这次只做出两题,其他题目,我也不知道什么时候才可以做出来、、、、
链接 :Codeforces Round #290 (Div. 2)
A. Pasha and Pixels
Pasha loves his phone and also putting his hair up... But the hair is now irrelevant.
Pasha has installed a new game to his phone. The goal of the game is following. There is a rectangular field consisting of n row with mpixels in each row. Initially, all the pixels are colored white. In one move, Pasha can choose any pixel and color it black. In particular, he can choose the pixel that is already black, then after the boy's move the pixel does not change, that is, it remains black. Pasha loses the game when a 2 × 2 square consisting of black pixels is formed.
Pasha has made a plan of k moves, according to which he will paint pixels. Each turn in his plan is represented as a pair of numbers iand j, denoting respectively the row and the column of the pixel to be colored on the current move.
Determine whether Pasha loses if he acts in accordance with his plan, and if he does, on what move the 2 × 2 square consisting of black pixels is formed.
The first line of the input contains three integers n, m, k (1 ≤ n, m ≤ 1000, 1 ≤ k ≤ 105) — the number of rows, the number of columns and the number of moves that Pasha is going to perform.
The next k lines contain Pasha's moves in the order he makes them. Each line contains two integers i and j (1 ≤ i ≤ n, 1 ≤ j ≤ m), representing the row number and column number of the pixel that was painted during a move.
If Pasha loses, print the number of the move when the 2 × 2 square consisting of black pixels is formed.
If Pasha doesn't lose, that is, no 2 × 2 square consisting of black pixels is formed during the given k moves, print 0.
2 2 4 1 1 1 2 2 1 2 2
4
2 3 6 2 3 2 2 1 3 2 2 1 2 1 1
5
5 3 7 2 3 1 2 1 1 4 1 3 1 5 3 3 2
0
每次涂色时,判断旁边的方块即可。
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
using namespace std;
#define N 1010
bool col[N][N];
int n, m, k;
bool judge(int x, int y)
{
if(col[x][y] && col[x + 1][y] && col[x][y - 1] && col[x + 1][y - 1])
return true;
else if(col[x][y] && col[x - 1][y] && col[x - 1][y - 1] && col[x][y - 1])
return true;
else if(col[x][y] && col[x][y + 1] && col[x - 1][y] && col[x - 1][y + 1] )
return true;
else if(col[x][y] && col[x][y + 1] && col[x + 1][y] && col[x + 1][y + 1])
return true;
else
return false;
}
int main()
{
int a, b;
while(~scanf("%d%d%d",&n,&m,&k))
{
memset(col, false, sizeof(col));
bool flag = false;
int ans = 0;
for(int t = 0; t < k ; t ++)
{
scanf("%d%d",&a, &b);
if(flag)
continue;
if(col[a][b])
continue;
col[a][b] = true;
if(judge(a, b))
flag = true;
if(flag)
ans = t + 1;
}
if(flag)
{
printf("%d\n",ans);
}
else
printf("0\n");
}
}
题目输入为奇数,要求使得调换其中两个位置上的数,使得该数变为偶数,并且输出该数的最大的可能值。既然是奇数,那就一定是个位数(设为maxn)和其他位上的偶数进行调换了。我们可以这样看,为了保持调换后的数值最大,从第一位数开始,找到第一个比maxn小的数,调换即可,如果没找到,那就找到最后一个偶数再调换即可。如果该数每个位上的数都是奇数,输出-1。
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <queue>
#include <vector>
#include <map>
using namespace std;
#define N 100010
char s[N];
int ans[10];
int main()
{
while(~scanf("%s",s))
{
memset(ans, 0, sizeof(ans));
int len = strlen(s);
bool ok = false, ok2 = false;
int ans = 0;
int maxn = s[len - 1] - '0';
for(int i = 0; i < len; i ++)
{
if(ok2)
continue;
int tmp1 = s[i] - '0';
if(tmp1 % 2 == 0)
{
if(tmp1 < maxn)
{
ans = i;
ok2 = true;
}
ok = true;
}
}
if(ans == 0 && !ok2)
{
for(int i = 0; i < len; i ++)
{
int tmp1 = s[i] - '0';
if(tmp1 % 2 == 0)
{
ans = i;
ok = true;
}
}
}
if(!ok)
{
printf("-1\n");
continue;
}
char tmp3;
tmp3 = s[ans];
s[ans] = s[len - 1];
s[len - 1] = tmp3;
for(int i = 0; i < len; i ++)
{
printf("%c",s[i]);
}
printf("\n");
}
return 0;
}