博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
随机行走
阅读量:3948 次
发布时间:2019-05-24

本文共 2100 字,大约阅读时间需要 7 分钟。

在这里插入图片描述

在这里插入图片描述
同学的代码,没看懂,我运行出来还是错的

#include
#include
#include
#include
using namespace std;void init_all(vector
>& all,int row){ all.resize(row); for(int i=0;i
()); all[i].resize(i*2+1,0); } }bool judge(vector
>& all,int& roof){ for(int i=0;i
>& all,int& roof){ for(int i=0;i
>& all,int& row,int& row,int &column,int& roof){ int target=rand()%4; cout<
<<" "<
<<" "<
<
>row; if(row==1){ cout<<"0行:1"<
> all; init_all(all,row); int now_row=0; int now_column=0; all[0][0]++; while(1){ change(all,now_row,now_column,row); if(judge(all,row)){ out(judge(all,row)); break; } }}

https://blog.csdn.net/hxxjxw/article/details/84986031#comments

下面代码是这个大佬的

//#include "stdafx.h"#include
#include
#include
#include
using namespace std;int a[10][10];int x, y;int random()//随机产生1~3范围中的一个数,代表走的方向{
return rand() % 4;}void process(int e)//e代表走的方向{
if (e == 1) {
if ((x & 1)==0 && (x >= 2 && y >= 2))//若当前点是该行第奇数个点三角形往下走,第偶数个点三角形往上走 x--, y--; else //还要判断是否越界 if (x <= 7 && y <= 7) x++, y++; } if (e == 2)//往左走 if (x >= 2)//还要保证不越界 x--; if (e == 3)//往右走 if (x <= 7)//还要保证不越界 x++;}bool judge()//判断所有三角形是否都已经走过{
for (int i = 1; i <= 8; i++) for (int j = 1; j <= i; j++) if (a[i][j] == 0) return 0; return 1;}void print(){
for (int i = 1; i <= 8; i++) {
for (int j = 1; j <= i; j++) cout << a[i][j] << ' '; cout << endl; }}int main(){
srand(time(0)); int T = 2; long long ans = 0; for(int i=1;i<=T;i++) {
cout << "第" << i << "次:\n"; int cnt = 0; x = 1; y = 1; memset(a, 0, sizeof(a)); while (1) {
process(random()); a[x][y]++; for (int i = 1; i <= 8; i++) {
for (int j = 1; j <= i; j++) cout << a[i][j] << ' '; cout << endl; } if (judge())//判断所有三角形是否都已经走过 {
print(); break; } else cnt++; } cout <<"总步数:" <
<< endl << endl; ans += cnt; } cout <<"平均次数为:"<< fixed << double(ans) / 100 << endl; system("pause"); return 0;}

在这里插入图片描述

T太大,导致很长时间运行不出来。

你可能感兴趣的文章
[数据库]事务、并发、数据库锁
查看>>
单例设计模式
查看>>
装饰设计模式和代理设计模式的区别
查看>>
Struts2中值栈
查看>>
Hash算法冲突解决方法分析
查看>>
网络地址和主机地址
查看>>
IP地址和子网掩码
查看>>
linux常用指令介绍
查看>>
AtomicInteger的CAS原理,及为啥不用synchronized而用cas
查看>>
Minor GC ,Full GC 触发条件
查看>>
数据结构中常见的树(BST二叉搜索树、AVL平衡二叉树、RBT红黑树、B-树、B+树、B*树)
查看>>
公平锁与非公平锁
查看>>
QL的四种连接-左外连接、右外连接、内连接、全连接
查看>>
常见加密算法
查看>>
Java遍历Map对象的四种方式
查看>>
scala学习之安装问题
查看>>
linux之shell倒引号,单引号,双引号,dirname,basename使用
查看>>
linux之shell文件路径切分,${str:a:b}用法
查看>>
Linux下如何查看版本信息
查看>>
LDAP常见错误码
查看>>