PKU 1643 Checker's Check

Bear。 发表于 2008-07-23 12:38:07

啊!!模拟题。搞定一道后的那个成就感啊~~~ 哇卡卡卡。~
小秀一下我的程序简短呀~  pku 程序长度。排 第三。 嘿嘿。~  ^^

#include <iostream>
#include <sstream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <vector>
using namespace std;

const int movex[4] = {-1, -1, 1, 1};
const int movey[4] = {-1, 1, 1, -1};
const int last[3] = {0, 7, 0};

struct POS {
  int x, y;

  POS () {}
  POS (int a, int b) { x = a, y = b; }
};

POS pos[33];
int G[8][8], num[8][8];
vector<POS> path;

void printG() {
  for (int i = 0; i < 8; i++) {
    for (int j = 0; j < 8; j++)
      printf("%3d", G[i][j]);
    cout << endl;
  }
  cout << "------------\n";
}

void piece(int n, int player) {
  int x, t;
  for (int i = 0; i < n; i++) {
    scanf("%d", &x);
    t = labs(x);
    G[pos[t].x][pos[t].y] = player * x / labs(x);
  }
}

bool getinf() {
  int r, w;

  scanf("%d%d\n", &r, &w);
  if (r + w == 0) return 0;
  memset(G, 0, sizeof(G));
  piece(r, 1);
  piece(w, 2);
  return 1;
}

void prepare() {
  int N = 0;

  memset(num, 0, sizeof(num));
  for (int i = 0; i < 8; i++)
    for (int j = 1 - (i & 1); j < 8; j += 2) {
      num[i][j] = ++N;
      pos[N] = POS(i, j);
    }
}

void runover(int st , int ed) {
  char s[100];

  for (int i = st; i <= ed; i++) gets(s);
}

void wrong(int x) {
  printf("Move %d is invalid\n", x);
}

bool inside(int x, int y) {
  return (x >= 0 && y >= 0 && x < 8 && y < 8);
}

bool canjump(int player, POS p, int d) {
  int x , y;

  if (labs(G[p.x][p.y]) != player) return 0;
  x = p.x + movex[d];
  y = p.y + movey[d];
  if (!inside(x, y) || labs(G[x][y]) != 3 - player) return 0;
  x += movex[d];
  y += movey[d];
  if (!inside(x, y) || G[x][y] != 0) return 0;
  return 1;
}

void getLR(int player, POS p, int &L, int &R) {
  if (G[p.x][p.y] < 0) L = 0, R = 3;
  else if (player == 1) L = 2, R = 3;
  else L = 0, R = 1;
}

void promotion(int player) {
  if (path.back().x == last[player]) G[path.back().x][path.back().y] = -player;
}

bool move(int player) {
  if (path.size() != 2) return 0;
  int L, R, x, y;

  for (int x = 0; x < 8; x++)
    for (int y = 0; y < 8; y++) {
      if (labs(G[x][y]) != player) continue;
      getLR(player, POS(x, y), L, R);
      for (int i = L; i <= R; i++)
 if (canjump(player, POS(x, y), i)) return 0;
    }

  getLR(player, path[0], L, R);
  for (int i = L; i <= R; i++) {
    x = path[0].x + movex[i];
    y = path[0].y + movey[i];
    if (x == path[1].x && y == path[1].y && !G[x][y]) {
      G[x][y] = G[path[0].x][path[0].y];
      G[path[0].x][path[0].y] = 0;
      promotion(player);
      return 1;
    }
  }
  return 0;
}

bool jump(int player) {
  if (path.size() < 2) return 0;
  int L, R, x, y;
  int tmp[8][8];
  bool found;

  memcpy(tmp, G, sizeof(G));
  getLR(player, path[0], L, R);
  for (int k = 1; k < path.size(); k++) {
    found = 0;
    for (int i = L; i <= R; i++) {
      if (canjump(player, path[k - 1], i)) {
 x = path[k - 1].x + movex[i] * 2;
 y = path[k - 1].y + movey[i] * 2;
 if (x == path[k].x && y == path[k].y) {
   x = path[k - 1].x + movex[i];
   y = path[k - 1].y + movey[i];
   G[x][y] = 0;
   x += movex[i];
   y += movey[i];
   G[x][y] = G[path[k - 1].x][path[k - 1].y];
   G[path[k - 1].x][path[k - 1].y] = 0;
   found = 1;
   break;
 }
      }
    }
    if (!found) {
      memcpy(G, tmp, sizeof(tmp));
      return 0;
    }
  }
  for (int i = L; i <= R; i++)
    if (canjump(player, path.back(), i)) {
      memcpy(G, tmp, sizeof(tmp));
      return 0;
    }
  promotion(player);
  return 1;
}

void getpath() {
  int x;
  char ch, s[500];

  gets(s);
  istringstream in(s);
  path.clear();
  in >> x; path.push_back(pos[x]);
  while (in >> ch >> x) path.push_back(pos[x]);
}

void solve() {
  int tot, player;
  char ch;

  scanf("%d %c\n", &tot, &ch);
  player = (ch == 'W') + 1;
  for (int i = 1; i <= tot; i++, player = 3 - player) {
    //    printG();
    getpath();
    if (labs(G[path[0].x][path[0].y]) != player || path.size() < 2 || !move(player) && !jump(player)) {
      runover(i + 1, tot);
      wrong(i);
      return;
    }
  }
  printf("All moves valid\n");
}

int main() {
  prepare();
  while (getinf()) solve();
}

关键词(Tag): 模拟 pku 1643

收藏: QQ书签 del.icio.us 订阅: Google 抓虾

最新评论

发表评论

* 昵称

已经注册过? 请登录

新用户请先注册 以便能显示头像及追踪评论回复

Email
网址
* 评论
表情
 
 

分类小组论坛
杂谈, 娱乐、八卦, 文学、艺术, 体育, 旅游、同城, 象牙塔, 情感, 时尚、生活, 星座, 科技

请注意遵守中华人民共和国法律法规, 如威胁到本站生存, 将依法向有关部门报告, 同时本站的相关记录可能成为对您不利的证据.

相关法律法规
全国人大常委会关于维护互联网安全的决定
中华人民共和国计算机信息系统安全保护条例
中华人民共和国计算机信息网络国际联网管理暂行规定
计算机信息网络国际联网安全保护管理办法
计算机信息系统国际联网保密管理规定