ランダムウォーク

ここに画像の説明を挿入
ここに画像の説明を挿入
クラスメートのコード、わかりません、それでも間違って実行します

#include<iostream>
#include<vector>
#include<cstdlib>
#include<ctime>
using namespace std;
void init_all(vector<vector<int>>& all,int row)
{
	all.resize(row);
	for(int i=0;i<row;i++)
	{
		all.push_back(vector<int>());
		all[i].resize(i*2+1,0);	
	}	
}
bool judge(vector<vector<int>>& all,int& roof)
{
	for(int i=0;i<roof;i++)
	{
		for(int j=0;i<all[i].size();j++)
			if(!all[i][j]) return false;
	}
	return true;
}
void out(vector<vector<int>>& all,int& roof)
{
	for(int i=0;i<roof;i++)
	{
		cout<<i<<"行: ";
		for(int j=0;j<all[i].size();j++) cout<<all[i][j]<<" ";
		cout<<endl; 
	}
}
void change(vector<vector<int>>& all,int& row,int& row,int &column,int& roof)
{
	int target=rand()%4;
	cout<<row<<" "<<column<<" "<<target<<endl;
	if(!target) all[row][column]++;
	if(target==1) column==0?all[row][column]++ : all[row][--column]++;
	if(target==2) column==row*2?all[row][column]++ : all[row][++column]++;
	if(target==3){
		if(column%2) all[--row][--column]++;
		else{
			if(row==roof-1) all[row][column]++;
			else all[++row][++column]++;
		}
	} 
}
int main()
{
	srand((int)time(0));
	int row;
	cin>>row;
	if(row==1){
		cout<<"0行:1"<<endl;
		return 0; 
	}
	if(row<1)
	{
		cout<<"row error"<<endl;
		return 1;
	}
	vector<vector<int>> 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<iostream>
#include<time.h>
#include<stdlib.h>
#include<cstring>
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 <<"总步数:" <<cnt << endl << endl;
		ans += cnt;
	}
	cout <<"平均次数为:"<< fixed << double(ans) / 100 << endl;
	system("pause");
	return 0;
}

ここに画像の説明を挿入
Tが大きすぎるため、実行時間が長くなります。

元の記事を44件公開 Likes2 訪問数540

おすすめ

転載: blog.csdn.net/qq_43699776/article/details/105290174