Codeforces Round #124 (Div. 2)

本文分享了作者参加 CodeForces 编程比赛的经历,并解析了几道题目的解题思路,包括 A 题的博弈策略、B 题的简单分类讨论、C 题的贪心算法及 D 题的 BFS 实现。


http://codeforces.com/contest/197


这次看到cf 九点多就开始了,很开心的注册下于是就悲剧了,快哭了

A题,题目开始看错了,以为每次只能放一个盘子,想了半天博弈都没想出来,之后看到别人的代码才发现。先手肯定尽可能的放满整个桌子,这样后手就必败;如果先手一个盘子都放不了,那么先手就输了;

B题   水题;分情况讨论;

C题   贪心,从后向前扫描贪心,如果遇到的字母 >=栈顶的字母 ,那么入栈;

D题  bfs,看的别人的代码:

#include <sstream>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<double> VD;
typedef vector<string> VS;
typedef vector<LL> VLL;

#define PB push_back
#define MP make_pair
#define foreach(e,x) for(typeof((x).begin()) e=(x).begin(); e!=(x).end(); ++e);

const int MAX_N = 1500 + 10;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

int N, M;
int sx, sy;
int qx[MAX_N * MAX_N], qy[MAX_N * MAX_N];
char a[MAX_N][MAX_N];
char vis[MAX_N][MAX_N];
PII rec[MAX_N][MAX_N];

int BFS(int sx, int sy)
{
	int qh = 0, qt = 0;
	qx[0] = sx; qy[0] = sy;
	vis[sx][sy] = 1; rec[sx][sy] = MP(sx, sy);
	qt ++;

	int nx, ny, px, py, lx, ly;

	for( ; qh < qt; )
	{
		nx = qx[qh];
		ny = qy[qh];
		++ qh;
		//cout<<"#"<<nx<<" "<<ny<<endl;
		for(int k = 0; k < 4; ++ k)
		{
			px = nx + dx[k];
			py = ny + dy[k];
			lx = (px % N + N) % N;
			ly = (py % M + M) % M;
			PII cur(px, py);

			if (a[lx][ly])
				continue;

			if(vis[lx][ly])
			{
				if(rec[lx][ly] != cur)
					return true;
			}
			else
			{
				vis[lx][ly] = true;
				rec[lx][ly] = cur;
				qx[qt] = px;
				qy[qt] = py;
				++ qt;
			}
		}
	}

	return false;
}

void solve()
{
	scanf("%d%d", &N, &M);
	for(int i = 0; i < N; ++ i) {
		scanf("%s", a[i]);
		for(int j = 0; j < M; ++ j) {
			if (a[i][j] == 'S')
				sx = i, sy = j;
			if (a[i][j] == '#')
				a[i][j] = 1;
			else a[i][j] = 0;
		}
	}

	if (BFS(sx, sy))
		puts("Yes");
	else
		puts("No");
}

int main()
{
	solve();
	return 0;
}
/*
3 3
...
###
#S#
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值