您的位置:首页 > 理论基础 > 计算机网络

2010 ACM/ICPC Online-Contest-SCU[四川赛区网络预选赛]

2010-09-25 13:30 357 查看
Problem A.A Simple Problem

比赛时唯一做出来的一道题。

描述:

Time limit: 1 second
Memory limit: 256 megabytes
There's one row of buttons lying on onmylove's laptop keyboard, which are used to input the numbers,
just as shown in the picture:

代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;

#define NL1 201
#define NL2 21
#define EP 1e-10
#define MAX(x,y) ((x)>(y)?(x):(y))
#define MIN(x,y) ((x)<(y)?(x):(y))
#define LL long long

int d[NL2][NL2], d1[NL2][NL2];
int dir[9][2] = {{0, -1}, {0, 1}, {-1, 0}, {-1, 1}, {-1, -1}, {1, 0}, {1, -1}, {1, 1}, {0, 0}};
char ch[NL1];

int main() {
int i, j, k;
//    freopen("in.txt", "r", stdin);
while (scanf("%s", ch+1) != EOF) {
int L = strlen(ch+1);
memset(d, -1, sizeof(d));
d[5][6] = 0;
int t=0;
bool suc = false;
while (1) {
t++;
memset(d1, -1, sizeof(d1));
for (i=1; i<=9; i++) {
for (j=i+1; j<=10; j++) {
if (d[i][j]>=0) {
for (k=0; k<9; k++) {
int i0 = i+dir[k][0];
int j0 = j+dir[k][1];
if (i0<j0 && i0>=1 && i0<=9 && j0>=2 && j0<=10) {
if (i == i0 && ch[d[i][j]+1] == i+'0') {
if (d1[i0][j0] == -1) {
d1[i0][j0] = d[i][j]+1;
}else {
d1[i0][j0] = MAX(d1[i0][j0], d[i][j]+1);
}
}
if (j==j0 && ch[d[i][j]+1] == ((j==10)?0:j)+'0') {
if (d1[i0][j0] == -1) {
d1[i0][j0] = d[i][j]+1;
}else {
d1[i0][j0] = MAX(d1[i0][j0], d[i][j]+1);
}
}
if (i!=i0 && j!=j0) {
if (d1[i0][j0] == -1) {
d1[i0][j0] = d[i][j];
}else {
d1[i0][j0] = MAX(d1[i0][j0], d[i][j]);
}
}
if (d1[i0][j0] == L) {
suc = true;
break;
}
}
}
if (suc) break;
}
if (suc) break;
}
if (suc) break;
}
for (i=0; i<=9; i++) {
for (j=i+1; j<=10; j++) {
d[i][j] = d1[i][j];
}
}
if (suc) break;
}
printf("%d\n", t);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐