2014年7月7日

UVa 12488 - Start Grid

#include <stdio.h>

int main() {
  int N;
  while (scanf("%d", &N) == 1) {
    int grid[2][99];
    int i, j, k;
    for (i = 0; i < N; i++) {
      scanf("%d", &grid[0][i]);
    }
    for (i = 0; i < N; i++) {
      scanf("%d", &grid[1][i]);
    }
    int count = 0;
    for (i = N - 1; i >= 0; i--) {
      for (j = 0; j < N; j++) {
        if (grid[0][j] == grid[1][i]) {
          for (k = j; k < i; k++) {
            int temp = grid[0][k + 1];
            grid[0][k + 1] = grid[0][k];
            grid[0][k] = temp;
            count++;
          }
          break;
        }
      }
    }
    printf("%d\n", count);
  }
  return 0;
}

2014年7月6日

UVa 677 - All Walks of length "n" from the first node

#include <cstdio>
#include <vector>

using std::vector;

int N, L;
bool solved;

vector<int> con[10], path;

void solve(int n, int len, bool * visited) {
  if (len == L) {
    solved = true;
    for (int i = 0; i < path.size(); i++) {
      printf("%s%d%s", (i == 0) ? "(" : "", path[i] + 1, (i == path.size() - 1) ? ")" : ",");
    }
    puts("");
    return;
  }
  for (int i = 0; i < con[n].size(); i++) {
    int j = con[n][i];
    if (!visited[j]) {
      visited[j] = true;
      path.push_back(j);
      solve(j, len + 1, visited);
      path.pop_back();
      visited[j] = false;
    }
  }
}

int main() {
  int temp = 0;
  do {
    if (temp) {
      puts("");
    }
    scanf("%d%d", &N, &L);
    for (int r = 0; r < N; r++) {
      con[r].clear();
      for (int c = 0; c < N; c++) {
        int ok;
        scanf("%d", &ok);
        if (ok) {
          con[r].push_back(c);
        }
      }
    }
    solved = false;
    path.clear();
    path.push_back(0);
    bool visited[10] = {true};
    solve(0, 0, visited);
    if (!solved) {
      printf("no walk of length %d\n", L);
    }
  } while (scanf("%d", &temp) == 1);
  return 0;
}

UVa 12700 - Banglawash

#include <stdio.h>

int main() {
  int T, C = 1;
  scanf("%d", &T);
  while (T--) {
    int N;
    scanf("%d", &N);
    char s[99];
    scanf("%s", s);
    int i, cnt[99] = {};
    for (i = 0; s[i]; i++) {
      cnt[s[i]]++;
    }
    printf("Case %d: ", C++);
    if (cnt['A'] == N) {
      puts("ABANDONED");
    } else if ((cnt['B'] + cnt['A']) == N) {
      puts("BANGLAWASH");
    } else if ((cnt['W'] + cnt['A']) == N) {
      puts("WHITEWASH");
    } else if (cnt['B'] == cnt['W']) {
      printf("DRAW %d %d\n", cnt['B'], cnt['T']);
    } else if (cnt['B'] > cnt['W']) {
      printf("BANGLADESH %d - %d\n", cnt['B'], cnt['W']);
    } else {
      printf("WWW %d - %d\n", cnt['W'], cnt['B']);
    }
  }
  return 0;
}