栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 2276 Lara Croft

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 2276 Lara Croft

#include <iostream>#include <iomanip>#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <string>#include <vector>#include <algorithm>using namespace std;#ifdef WIN32#define for if(0); else for#endifint n;int a[100];int p1, p2;int tt[100][100];int alist[100000], blist[100000];int head, tail;int bfs() {int step = 0;int nhead, ntail;while (1) {nhead = tail + 1;ntail = tail;step ++;for (int i = head; i <= tail; i ++) {int ta, tb;int temp = a[blist[i]] % n;ta = alist[i], tb = (blist[i] + temp) % n;if (tt[ta][tb] == 0) {tt[ta][tb] = tt[tb][ta] = 1;ntail ++;alist[ntail] = ta, blist[ntail] = tb;}tb = (blist[i] - temp + n) % n;if (tt[ta][tb] == 0) {tt[ta][tb] = tt[tb][ta] = 1;ntail ++;alist[ntail] = ta, blist[ntail] = tb;}temp = a[alist[i]] % n;ta = (alist[i] + temp) % n, tb = blist[i];if (tt[ta][tb] == 0) {tt[ta][tb] = tt[tb][ta] = 1;ntail ++;alist[ntail] = ta, blist[ntail] = tb;}ta = (alist[i] - temp + n) % n;if (tt[ta][tb] == 0) {tt[ta][tb] = tt[tb][ta] = 1;ntail ++;alist[ntail] = ta, blist[ntail] = tb;}}for (int i = 0; i < n; i ++) {if (tt[i][i] == 1) return step;}if (ntail < nhead) break;for (int i = nhead; i <= ntail; i ++) {alist[i - nhead] = alist[i];blist[i - nhead] = blist[i];}head = 0, tail = ntail - nhead;}return -1;}int main () {while (scanf("%d", &n) != EOF) {if (n == 0) break;for (int i = 0; i < n; i ++) scanf("%d", &a[i]);scanf("%d %d", &p1, &p2);for (int i = 0; i < n; i ++) {for (int j = 0; j < n; j ++) tt[i][j] = 0;}tt[p1][p2] = tt[p2][p1] = 1;head = tail = 0;alist[head] = p1;blist[head] = p2;if (p1 == p2) {printf("open it on the %dth move!n", 0);continue;}int res = bfs();if (res == -1) printf("Lara is traped!n");else printf("open it on the %dth move!n", res);}return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/377644.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号