月赛两道能A的题

#include<cstdio>
#include<cstring>
#include<cstdlib>
int n, m, x1, y1, x2, y2;
int dx[] = { -1, -1, 1, 1, 2, 2, -2, -2};
int dy[] = { 2, -2, 2, -2, 1, -1, 1, -1};
int d[25][25], qx[500], qy[500];

int main()
{
int x, y, front, rear;
while( scanf( "%d%d", &n, &m) == 2)
{
scanf( "%d%d%d%d", &x1, &y1, &x2, &y2);
memset( d, -1, sizeof d);
d[x2][y2] = 0;
front = rear = 0;
qx[rear] = x2;
qy[rear] = y2;
rear ++;
while( front < rear)
{
x = qx[front];
y = qy[front];
front ++;
if( x == x1 && y == y1)
break;
for( int i = 0; i < 8; i ++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if( d[nx][ny] < 0 && nx >= 1 && nx <= n && ny >= 1 && ny <= m)
{
d[nx][ny] = d[x][y] + 1;
qx[rear] = nx;
qy[rear] = ny;
rear ++;
}
}
}
printf( "%d\n", d[x1][y1]);
}
return 0;
}
#include<cstdio>
#include<cstring>
#include<cstdlib>
#define MAXN 10005
int A[MAXN], f[MAXN], t[MAXN];
int main()
{
int n;
while( scanf( "%d", &n) == 1)
{
for( int i = 1; i <= n; i ++) {
scanf( "%d", &A[i]);
f[i] = 1;
}
memset( t, 0, sizeof t);
for( int i = 1; i <= n; i ++) {
for( int j = 1; j < i; j ++)
{
if( A[i] > A[j] && f[i] < f[j] + 1)
f[i] = f[j] + 1;
}
for( int j = 1; j < i; j ++)
{
if( A[i] > A[j] && f[i] == f[j] + 1)
t[i] += t[j];
}
if( t[i] == 0) t[i] = 1;
}
int max = 0, ans = 0;
for( int i = 1; i <= n; i ++)
{
if( f[i] > max)
{
max = f[i];
ans = 0;
}
if( f[i] == max)
ans += t[i];
}
printf( "%d %d\n", max, ans );
}
return 0;
}

开始居然一点想法都没有,居然用了深搜...

你可能感兴趣的:(a)