题目大意

智能手机九点屏幕滑动解锁,如果给出某些连接线段,求出经过所有给出线段的合法的滑动解锁手势的总数。题目链接: 
滑动解锁

题目分析

首先,尝试求解没有给定线段情况下,所有合法的路径的总数。可以使用dfs进行搜索。代码如下:

void dfs(int row, int col, int cur_len) {
visited[row][col] = true;
if (cur_len >= 4) { //到达该点时,走过的路径长度大于等于4,则为合法的一个解锁手势
total_count++;
}
if (cur_len == 10)
return;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int next_row = (row + i) % 3;
int next_col = (col + j) % 3;
if (!visited[next_row][next_col]) {
//可能出现当前点和下一个点的连线上经过了另一个点的情况,进行判断。如果
//经过的另一个点之前被访问过,则这次仍然是合法的。
if (abs(row - next_row) == 2 && abs(col - next_col) != 1 ||
abs(col - next_col) == 2 && abs(row - next_row) != 1) {
int mid_row = (row + next_row) / 2;
int mid_col = (col + next_col) / 2;
if (!visited[mid_row][mid_col]) {
continue;
}
}
int cur = 3 * row + col;
int next = 3 * next_row + next_col;
dfs(next_row, next_col, cur_len + 1, need_use, cur_used + 1);
}
}
}
visited[row][col] = false;
}

在上面的dfs搜索基础上,添加对已有线段的限制。9个点,维护 connected[9][9], connected[i][j] 表示已经有线段将i和j连接。搜索的时候,还需要维护状态,cur_used表示当前已经经过了已有线段的数目,如果已经经过了所有的线段。且当前路径经过点数大于等于4,则是一个合法的解锁路径。

实现

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
bool visited[3][3];
bool connected[9][9];
int total_count;
//cur_len 为到达row,col点时候,路径的长度; cur_used表示已经走过的路径所覆盖的已有线段的个数
void dfs(int row, int col, int cur_len, int need_use, int cur_used) {
visited[row][col] = true;
if (cur_len >= 4 && need_use == cur_used) {
total_count++;
}
if (cur_len == 10)
return;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int next_row = (row + i) % 3;
int next_col = (col + j) % 3;
if (!visited[next_row][next_col]) {
if (abs(row - next_row) == 2 && abs(col - next_col) != 1 ||
abs(col - next_col) == 2 && abs(row - next_row) != 1) {
int mid_row = (row + next_row) / 2;
int mid_col = (col + next_col) / 2;
if (!visited[mid_row][mid_col]) {
continue;
}
}
int cur = 3 * row + col;
int next = 3 * next_row + next_col;
if(connected[cur][next])
dfs(next_row, next_col, cur_len + 1, need_use, cur_used + 1);
else
dfs(next_row, next_col, cur_len + 1, need_use, cur_used);
}
}
}
visited[row][col] = false;
}
void GetCount(int need_use) {
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
dfs(row, col, 1, need_use, 0);
}
}
}
int main() {
memset(visited, false, sizeof(visited));
int T, need_use, u, v;
scanf("%d", &T);
while (T--) {
total_count = 0;
memset(connected, false, sizeof(connected));
scanf("%d", &need_use);
for (int i = 0; i < need_use; i++) {
scanf("%d %d", &u, &v);
connected[u-1][v-1] = connected[v-1][u-1] = true;
}
GetCount(need_use);
printf("%d\n", total_count);
}
return 0;
}

hiho_1054_滑动解锁的更多相关文章

  1. hihocoder#1054 : 滑动解锁(深度优先搜索)

    描述 滑动解锁是智能手机一项常用的功能.你需要在3x3的点阵上,从任意一个点开始,反复移动到一个尚未经过的"相邻"的点.这些划过的点所组成的有向折线,如果与预设的折线在图案.方向上 ...

  2. Swift: 打造滑动解锁文字动画

    原文:Swift: 打造滑动解锁文字动画 最近木事,找出来玩了玩facebook的paper.到处都是那个"slide to unlock your phone"的效果啊.忽闪忽闪 ...

  3. Selenium模拟JQuery滑动解锁

    滑动解锁一直做UI自动化的难点之一,我补一篇滑动解锁的例子,希望能给初做Web UI自动化测试的同学一些思路. 首先先看个例子. https://www.helloweba.com/demo/2017 ...

  4. hihocoder 1054 滑动解锁 dfs

    详细分析见滑动解锁分析 AC代码 #include <cstdio> #include <cmath> #include <cctype> #include < ...

  5. C语言 · 滑动解锁

    题目:滑动解锁 滑动解锁是智能手机一项常用的功能.你需要在3x3的点阵上,从任意一个点开始,反复移动到一个尚未经过的"相邻"的点.这些划过的点所组成的有向折线,如果与预设的折线在图 ...

  6. jq实现简单的滑动解锁效果

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  7. APP九宫格滑动解锁的处理

    写手机自动化测试脚本关于APP九宫格滑动解锁方面采用了appium API 之 TouchAction 操作. 先是用uiautomatorviewer.bat查询APP元素坐标: 手工计算九宫格每个 ...

  8. 【转】Selenium模拟JQuery滑动解锁

    滑动解锁一直做UI自动化的难点之一,我补一篇滑动解锁的例子,希望能给初做Web UI自动化测试的同学一些思路. 首先先看个例子. https://www.helloweba.com/demo/2017 ...

  9. css3做ipone当时的滑动解锁闪亮条

    现在一般的登录 注册 什么  的页面,都是会做个滑动验证.一般都是像IPONE早期那个 滑动开屏的效果 ,这个效果现在可以用CSS3来实现. 主要用到几个属性 background 背景使用渐变属性, ...

随机推荐

  1. 【20160924】GOCVHelper 图像增强部分(4)

    //使得rect区域半透明     Mat translucence(Mat src,Rect rect,int idepth){         Mat dst = src.clone();     ...

  2. MySQL bin-log 日志清理方式

    MySQL bin-log 作用   1.数据恢复:如果你的数据库出问题了,而你之前有过备份,那么可以看日志文件,找出是哪个命令导致你的数据库出问题了,想办法挽回损失. 2.主从服务器之间同步数据:主 ...

  3. 测试post

    http://www.atool.org/httptest.php 这里也可以测试 火狐有插件:HttpRequester Chrome有插件:Postman

  4. 项目文件中含有两个config文件,app.config与app1.config,如何获取app1.config中的配置

    想要通过配置文件配置C#前台画面,好奇做了以下测试:在项目中新建了app.config与app1.config两个配置文件,请教一下各位高手如果想从app1.config中读取配置信息应该如何读取?采 ...

  5. php使用 set_include_path

    通过set_include_path引用home/lib/image.func.php 1.创建include.php 2.添加如下代码 3.在需要引用的文件中包含include.php文件 < ...

  6. c++类中的常量

    C++类中的常量 由于#define 定义的宏常量是全局的,不能达到目的,于是想当然地觉得应该用 const 修饰数据成员来实现.const 数据成员的确是存在的,但其含义却不是我们所期望的.cons ...

  7. Android 4.4之后删除短信进行处理

    android 4.4删除短信 android 4.4之后非默认的短信应用已经没有办法删除短信了.像以前那样用如下方法是不会没法删除短信的(即使在xml中配置了短信的读写权限),同时也不会有报错或其他 ...

  8. Python3基础 print 输出hello world

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...

  9. UVA 1291 十四 Dance Dance Revolution

    Dance Dance Revolution Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Su ...

  10. Difference between Char.IsDigit() and Char.IsNumber() in C#

    http://stackoverflow.com/questions/228532/difference-between-char-isdigit-and-char-isnumber-in-c-sha ...