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

3
bbwbwbwbw
bwwwbwbwb
bbbbwbbbw

Sample Output

2459
267
356789

看了别人的代码,自己打了一遍,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 1048. Inverso相关推荐

  1. sicily 1048 Inverso

    2019独角兽企业重金招聘Python工程师标准>>> Description The game of 'Inverso' is played on a 3x3 grid of co ...

  2. sicily题目分类

    sicily题目分类 1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. ...

  3. [sicily]部分题目分类

    sicily题目分类 1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. ...

  4. Sicily 题目分类

    依照自己水平挑着做→ →~~ 1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 ...

  5. 中大SICILY分类

    原文出处:http://linguifan2010.blog.163.com/blog/static/1315127442010102131322482/ ********************** ...

  6. Soj题目分类 python代码)

    正值期末复习,刷点soj放松下 但想看看能不能在找点关于数据结构的题目来做一下. 在网上看到有不少人上传过那些关于部分SOJ题目的描述,但是说实话有些乱 不过我看到有个网页中包含的一个类似文档的东西, ...

  7. 初学者acm的练习题指南

    上机练习题参考题 忘了在哪找的啦~~希望对大家有帮助呦 <!--[if !supportLists]-->1.    <!--[endif]-->Programming Bas ...

  8. 编程题目分类(剪辑)

    1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. 模拟 12. 算术与代 ...

  9. NYOJ题目1048破门锁

    -------------------------------------------------- 每个锁位上可能的数为5(正确的一个+上偏移的两个+下偏移的两个),所以总共可能性为: 但是这里面有 ...

最新文章

  1. 建军92周年,让我们了解那些先进的军用机器人
  2. shell实例第17讲:连续输入4个100以内的数字,统计和、乘、平均、最小和最大
  3. Elasticsearch技术解析与实战(一)基础概念及环境搭建
  4. 机器翻译(信息学奥赛一本通-T1401)
  5. 用Python操作MySQL(pymysql)
  6. Ubuntu Mate 开机自启ROS
  7. 杭电2067小兔的棋盘
  8. 【优化预测】基于matlab差分算法优化ANN预测【含Matlab源码 151期】
  9. 点击弹窗播放视频代码 !
  10. 物体检测模型RFBNet——一个非常好用的模型。
  11. 物联网专业要学c语言吗,物联网应用技术专业是文科还是理科
  12. hr面试性格测试30题_HR经典面试30题
  13. 【win7安装composer错误】:The quot;https://getcomposer.org/download/1.6.2/composer.phar.sigquot; file cou
  14. 出差经历的人在囧途那一天
  15. 港股通不得不了解的汇率问题
  16. 原码、反码、补码、移码存在的意义
  17. 华为手机自带浏览器无法下载 iis 网站 apk 问题解决方案(和SSL有关)
  18. java swing组件_Java -- Swing 组件使用
  19. Realtek RTL8367SC-CG(替代RTL8367S的新方案) datasheet及用途功能简介
  20. 春季出游将至 Bingdata大数据详解春季踏青游趋势

热门文章

  1. C# DirectoryInfo GetFiles()获得的文件列表与本机下文件顺序一致
  2. JMeter-16-循环控制器
  3. Computer Graphics Through OpenGL From Theory to Experiments - 学习笔记2 Tricks of the Trade opengl基础
  4. 微信企业号开发实例源码
  5. git、github保姆级教程(手把手交)以及如何在github上提交pr,参与开源项目
  6. 出入库管理系统1(配件报表-后录编号)
  7. openstack基准测试项目Rally介绍
  8. C语言及程序设计(公开课)主页
  9. Kafka : Kafka入门教程和JAVA客户端使用
  10. 战地服务器名字不显示号码的电话软件,打电话显示虚拟号码的软件,教你打电话隐藏号码...