Sicily 1048. Inverso
来源:互联网 发布:java反射原理 编辑:程序博客网 时间:2024/05/21 09:41
1048. Inverso
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
The game of ‘Inverso’ is played on a 3x3 grid of colored fields (each field is either black or white). Each field is numbered as follows:
1 2 3
4 5 6
7 8 9
The player can click on a field, which will result in the inversion of that field and all its direct neighboring fields (i.e. the color changes from black to white or vice-versa). Hence,
Clicking field 1 inverts fields 1, 2, 4, 5
Clicking field 2 inverts fields 1, 2, 3, 4, 5, 6
Clicking field 3 inverts fields 2, 3, 5, 6
Clicking 4 inverts fields 1, 2, 4, 5, 7, 8
Clicking 5 inverts fields all fields
Clicking 6 inverts fields 2, 3, 5, 6, 8, 9
Clicking 7 inverts fields 4, 5, 7, 8
Clicking 8 inverts fields 4, 5, 6, 7, 8, 9
Clicking 9 inverts fields 5, 6, 8, 9
The aim of the game is to find the shortest sequence of clicks to make all fields white from a given start coloring of the grid.
Input
The first line contains a number N (0≤N≤10000) of runs. The following N lines each contain a string of nine letters ‘b’ (black) or ‘w’ (white) for the color of the fields 1 to 9. This is the initial coloring of the grid.
Output
For each input string the shortest word in the letters ‘1’… ‘9’ (for clicking field one, …, nine) which makes all fields white. If there is more than one shortest word then the lexicographically smallest one must be printed (‘1234’ is smaller than ‘1342’).
Sample Input
3bbwbwbwbwbwwwbwbwbbbbbwbbbw
Sample Output
2459 267356789
看了别人的代码,自己打了一遍,BFS,找出所有状态(2^9 = 512);
#include <stdio.h>#include <queue>#include <vector>#include <math.h>using namespace std;int d[10];int ans[512];vector<char> ans_step[512];int opp[9][9] = {1, 2, 4, 5, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 0, 0, 0, 2, 3, 5, 6, 0, 0, 0, 0, 0, 1, 2, 4, 5, 7, 8, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 5, 6, 8, 9, 0, 0, 0, 4, 5, 7, 8, 0, 0, 0, 0, 0, 4, 5, 6, 7, 8, 9, 0, 0, 0, 5, 6, 8, 9, 0, 0, 0, 0, 0};bool ok() { for (int i = 0; i < 9; i++) { if (d[i] == 1) return false; } return true;}struct step { int num; vector<char> v; step(){} step(int n) { num = n; } step(int n, char next_step) { num = n; v.push_back(next_step); } step(int n, vector<char> temp) { num = n; for (int i = 0; i < (int)temp.size(); i++) { v.push_back(temp[i]); } }};int change(bool test[], int op) { bool test_next_step[10]; for (int i = 0; i < 9; i++) { test_next_step[i] = test[i]; } for (int i = 0; i < 9 && opp[op][i]; i++) { test_next_step[opp[op][i] - 1] = test[opp[op][i] - 1] ^ 1; } int sum = 0; for (int i = 0; i < 9; i++) { sum += test_next_step[i] * (int)pow(2, i); } return sum;}void BFS() { queue<step> q; q.push(step(0)); int size; step temp, next; while (!q.empty()) { size = q.size(); while (size--) { temp = q.front(); q.pop(); bool test[10]; int temp_num = temp.num; for (int i = 0; i < 9; i++) { test[i] = temp_num % 2; temp_num /= 2; } for (int i = 8; i >= 0; i--) { int next_num = change(test, i); if (ans_step[next_num].empty()) { for (int j = 0; j < (int)temp.v.size(); j++) { ans_step[next_num].push_back(temp.v[j]); } ans_step[next_num].push_back(i + '1'); q.push(step(next_num, ans_step[next_num])); } } } }}int main() { int case_num; BFS(); ans_step[0].clear(); ans_step[0].push_back('1'); ans_step[0].push_back('1'); char temp[10]; scanf("%d\n", &case_num); while (case_num--) { int index = 0; gets(temp); for (int i = 0; i < 9; i++) { index += (temp[i] == 'w' ? 0 : 1) * (int)pow(2, i); } for (int i = (int)ans_step[index].size() - 1; i >= 0; i--) { printf("%c", ans_step[index][i]); } printf("\n"); } return 0;}
- [sicily online]1048. Inverso
- sicily 1048. Inverso
- Sicily 1048. Inverso
- Sicily 1048 Inverso
- sicily 1048 Inverso
- 1048. Inverso
- 1048. Inverso
- 1048 Inverso
- 1048 Inverso
- Sicily 1027
- sicily 1007
- sicily 1795
- sicily 1036
- sicily 1419
- sicily 1889
- sicily 1684
- sicily 1686
- sicily 1004
- mongodb —java
- uva271
- (4.4.7)android其他类
- OC-04值对象
- nanomsg pair tcp SERVER端各个模块的状态
- Sicily 1048. Inverso
- 实现字符串循环右移n 位与左移n位(不建立数组,直接用指针)
- hdu 1716 排列2(全排列+dfs)
- 【c语言】判断1000年---2000年之间的闰年
- linux常用基本命令
- 选择排序函数的计时程序
- 在xcode系统的库文件里面进行了操作造成的bug
- Android HAL实例解析---LED
- jQuery插件开发全解析