题目大意

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

题目分析

首先,尝试求解没有给定线段情况下,所有合法的路径的总数。可以使用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. Unity重力的测试

    其实和想象的结果还是有点出入 斜坡测试 (非悬空,直接把地面旋转成斜坡) 在非悬空状态下是不会受到斜坡的影响,只有进入悬空再进入斜坡,才会滑下去 一帧内关闭碰撞器再打开,或者一帧内打开Trigger再 ...

  2. Unity ScriptableObject的使用

    ScriptableObject主要实现对象序列化的保存,因为是Unity自己的序列化,所以比xml,json序列化方便很多,但相对可控性也比较差 1.Editor下写入和读取测试: using Un ...

  3. JAVA基础知识之List集合

    List接口和ListIterator接口介绍 List集合新增方法 List集合判断元素重复的标准 ListIterator List.ArrayList和List.Vector 固定长度的List ...

  4. 提高开发效率的 Eclipse 实用操作

    工欲善其事,必先利其器.对于程序员来说,Eclipse便是其中的一个“器”.本文会从Eclipse快捷键和实用技巧这两个篇章展开介绍.Eclipse快捷键用熟后,不用鼠标,便可进行编程开发,避免鼠标分 ...

  5. JavaScript的构造器与对象(二)

    constructor 的用法:对象的构造函数  每一个函数的Prototype属性指向的对象都包含唯一一个不可枚举属性constructor,该属性的值是这么一个对象:它指向了它所在的构造函数. 语 ...

  6. 2016年11月24日 星期四 --出埃及记 Exodus 20:15

    2016年11月24日 星期四 --出埃及记 Exodus 20:15 "You shall not steal.不可偷盗.

  7. flash视频器播放器代码

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  8. 如何快速清除.svn文件

    Windows Registry Editor Version 5.00[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell\清除SVN信息] @=&qu ...

  9. Cache缓存对象缓存对象

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DemoCache.aspx ...

  10. window删除损坏无法打开的文件

    移动硬盘删除文件时提示“文件或目录损坏且无法读取”的解决方法-chkdsk 命令的巧用 新买一个移动硬盘,同学借去Copy一个游戏,拷来后发现数据包损坏,提示"文件或目录损坏且无法读取&qu ...