您的位置:首页 > 其它

比赛总结,做题要细心!

2012-07-25 15:25 295 查看
今天的集训中期比赛暴露出我的很多问题,对于字符串处理的不熟悉,致使B题没写对,对于

最水的两道枚举和贪心,也是一共WA了五次,在四十几分钟之后才过的。H题的BFS思路没错,

关键代码也没写错,结果因为重复使用了变量,一直没检查出来,过了样例就胡乱提交题,

结果WA了六次,赛后才过掉。H题几乎用了三个小时,结果问题出在这个地方,心里真的不好

受!

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>
using namespace std;

const int MAXN = 105;
const int dx[] = { 0, 0, 1, -1};
const int dy[] = { 1, -1, 0, 0};

int map[MAXN][MAXN];
char s[MAXN];
int d[MAXN][MAXN];
int n, Sx, Sy, Ex, Ey;
typedef pair<int , int> pii;

void init()
{
int i, j;
for( i = 1; i <= n; i ++)
{
scanf( "%s", s);
for( j = 1; j <= n; j ++)
{
if( s[j - 1] == 'S')
{
map[i][j] = 0;
Sx = i, Sy = j;
}
else if( s[j - 1] == 'E')
{
map[i][j] = 0;
Ex = i, Ey = j;
}
else map[i][j] = s[j - 1] - '0';
}
}
}

void bfs()
{
int x, y, nx, ny, i, j, k;
memset( d, -1, sizeof d);
queue<pii> q;
pii u;
d[Sx][Sy] = 0;
u.first = Sx, u.second = Sy;
q.push(u);
while( !q.empty())
{
u = q.front();
x = u.first, y = u.second;
q.pop();
for( k = 0; k < 4; k ++) //开始这里是i
{
nx = x + dx[k];
ny = y + dy[k];
if( d[nx][ny] < 0 && map[nx][ny] != 1 && nx >= 1 && nx <= n && ny >= 1 && ny <= n)
{
d[nx][ny] = d[x][y] + 1;
if( nx == Ex && ny == Ey) return;
u.first = nx, u.second = ny;
q.push(u);
if( map[nx][ny] != 0)
{
for( i = 1; i <= n; i ++) //这里也是i WA了六次
for( j = 1; j <= n; j ++)
{
if( map[nx][ny] == map[i][j]){
if( d[i][j] == -1 || d[i][j] > d[nx][ny])
{
d[i][j] = d[nx][ny];
u.first = i, u.second = j;
q.push(u);
}
}
}
}

}
}
}
}

int main()
{
while( scanf( "%d", &n) == 1)
{
init();
bfs();
if(d[Ex][Ey] < 0)
printf( "Oh No!\n");
else
printf( "%d\n", d[Ex][Ey]);
}
return 0;
}


B题的字符串处理,自己写老是不对,参考标程就对了,这方面还要提高:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>

char buf1[300], buf2[300], *a, *b;
char p[100];

int main()
{
int i, j, t;
while( gets(buf1) && gets(buf2))
{
a = buf1, b = buf2;
sscanf( buf1, "%s", p);
printf( "%s", p);
for( a = strchr( a + 1, '\t'); a; a = strchr(a + 1, '\t'), ++ b)
{
printf("\t");
b = strchr(b, '\t');
if(*(a + 1) != '\t' && *(a + 1))
{
sscanf(a, "%s", p), printf("%s", p);
if(*(b + 1) != '\t' && *(b + 1))
sscanf(b, "%s", p), printf("%s", p);
}
}
printf( "\n");
}
return 0;
}


E题是最短路的变种,用堆优化的Dij就能过,当时不知道怎么看成回溯的,做题还是太少了。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
using namespace std;

typedef pair<int, int> pii;
const int MAXN = 5005;
const int MAXM = 20005;
const int inf = 0x3f3f3f3f;
int n, e, m;
int cost[MAXM], dist[MAXN];
int pnt[MAXM], head[MAXN], nxt[MAXM], vis[MAXN];

void addedge( int u, int v, int c)
{
pnt[e] = v, cost[e] = c, nxt[e] = head[u], head[u] = e ++;
}

void ReadGraph()
{
int i, a, b;
e = 0;
memset( head, -1, sizeof head);
for( i = 0; i < m; i ++)
{
scanf( "%d%d", &a, &b);
addedge( a, b, 0);
addedge( b, a, 1);
}
}

void dijkstra()
{
priority_queue< pii, vector<pii>, greater<pii> > q;
pii u;
int i, x, t, y;
memset( vis, false, sizeof vis);
for( i = 1; i <= n; i ++)
dist[i] = inf;
dist[1] = 0;
q.push( pii( 0, 1));
while( !q.empty())
{
u = q.top(); q.pop();
x = u.second, y = u.first;
if( vis[x]) continue;
vis[x] = true;
for( t = head[x]; t != -1; t = nxt[t])
{
if( dist[pnt[t]] > y + cost[t])
{
dist[pnt[t]] = y + cost[t];
q.push( pii( dist[pnt[t]], pnt[t]));
}
}
}
}

int main()
{
while( scanf( "%d%d", &n, &m) == 2)
{
ReadGraph();
dijkstra();
if( dist
== inf)
printf( "-1\n");
else
printf( "%d\n", dist
);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: