首页 > 其他分享 >八数码问题(蒟蒻打卡)

八数码问题(蒟蒻打卡)

时间:2023-04-25 20:23:47浏览次数:49  
标签:string int 问题 start 数码 && 打卡

原题:AcWing 845. 八数码 - AcWing

思路:用string储存状态bfs爆搜

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int bfs(string start)
 4 {
 5     int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};
 6     string end="12345678x";
 7     queue<string> q;
 8     unordered_map<string,int> d;
 9     q.push(start);
10     d[start]=0;
11     while(q.size())
12     {
13       
14         string t=q.front();q.pop();
15         if(t==end) return d[t];
16         int distance = d[t];
17         int k=t.find('x');
18         int x=k/3,y=k%3;
19         for(int i=0;i<4;i++)
20         {
21             int a=x+dx[i],b=y+dy[i];
22             if(a>=0 && a<3 && b>=0 && b<3)
23             {
24                 swap(t[k],t[a*3+b]);
25                 if(!d.count(t))
26                 {
27                     d[t]=distance+1;
28                     q.push(t);
29                 }
30                 swap(t[k],t[a*3+b]);
31             }
32         }
33     }
34     return -1;
35 }
36 int main()
37 {
38     char c;string start;
39     for(int i=0;i<9;i++)
40     {
41         cin>>c;
42         start+=c;
43     }
44     cout<<bfs(start)<<endl;
45     return 0;
46 }

 

标签:string,int,问题,start,数码,&&,打卡
From: https://www.cnblogs.com/surfing-suantou/p/17353720.html

相关文章

  • c++打卡第十五天
    一、问题描述 二、设计思路。①、我们在此使用结构体定义结构体数组,结构体数组中包括每个阶段的征税始末,以及相对应的税率。当我们将工资传入时,会出现相应阶段的部分,以及总共应需缴纳金额。②、我们设计计算函数,通过for循环进行计算各个阶段的计算,同时使用选择语句,判断工资是......
  • 第十一天第二个问题
    问题描述:以点类Point及平面图形类Plane为基类公有派生圆类Circle,main(void)函数完成对其的测试。Point类结构说明:Point类的数据成员包括:①私有数据成员:X坐标x(double型),Y坐标y(double型)。Point类成员函数包括:①有参构造函数Point(double,double)和拷贝构造函数Point(const......
  • 第十一天第一个问题
    问题描述:编写模板函数max5(),他将由一个T类型元素组成的数组作为参数,并返回数组中最大的元素(由于长度固定,因此可以在循环中使用硬编码,而不必通过参数来传递)。在一个程序中使用该函数,将T替换为一个包含5个int值的数组和一个包含5个double值的数组,以测试该函数。解决方法:1.建立一个模......
  • 深度学习--RNN实战与存在问题
    深度学习--RNN实战与存在问题时间序列预测importnumpyasnpimporttorchimporttorch.nnasnnimporttorch.optimasoptimfrommatplotlibimportpyplotasplt#数量num_time_steps=50#输入的维度input_size=1#隐藏层大小hidden_size=16#输出的维......
  • 每日打卡一小时(第十六天)
    一.问题描述 二.设计思路1.利用数组输入数据2.创建一个二维数组利用循环记录每组数据前面的值除以某个数等于后面的值的数3.记录每组的最大值和最小值4.最大值中找最小值,最小值中找最大值5.输出三.流程图 四.代码实现#include<iostream>usingnamespacestd;int......
  • C++每日打卡
    计算年龄问题定义一个Birthday类,其成员变量有3个整形变量(出生的年月日):year,month,day;提供构造方法对这3个成员变量进行初始化;成员函数有getAge(),其功能是实现计算到2017年12月25日时该Birthday对象的年龄。 #include<iostream>usingnamespacestd;classBirthday{int......
  • 4.25打卡
    #include<iostream>#include<iomanip>#include<cmath>usingnamespacestd;boolsymm(unsignedn){unsignedi=n;unsignedm=0;while(i>0){m=m*10+i%10;i/=10;}returnm==n;}intmain(){......
  • 个人所得税问题
     分析:设计一个结构体,;里面陈放结构体的征税起点,征税终点,征税率。定义一个结构体数组,将各个范围的征税格式存入该数组。定义一个函数calculate来计算个人所得税#include<stdio.h>#definetaxbase3500/*定义结构体*/typedefstruct{longstart;longend;doubletaxr......
  • 打卡2
    问题描述:假设银行一年整存零取的月息为0.63%。现在某人手中有一笔钱,他打算在今后的五年中的每年年底取出1000元,到第五年时刚好取完,请算出他存钱时应存多少。流程图: 伪代码:money=0fori<-1to5money=(money+1000)/1+12*0.063outputmoney代码:#include<iostream>usin......
  • 兔子产子问题
    问题描述有一对兔子,从出生后的第3个月起每个月都生一对兔子。小兔子长到第3个月后每个月又生一对兔子,假设所有的兔子都不死,问30个月内每个月的兔子总数为多少?代码如下#include<iostream>usingnamespacestd;intmain(){ longintfib1=1,fib2=1; longfib; cout<<f......