ペントミノ(pentomino)の改良版 [C++]
// pentomino
//
// 2012.03.18 ───── coded by 心如
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
const Bw = 16; // Board-Width
const Bh = 12; // Board-Height
const Sw = 6; // Space-Width
const Sh = 10; // Space-Height
const Sp = Bw + 1; // Start-Point
const Mp = Sh * Bw + Sw;
const Fv = 13; // frame_value
//チップパターンのデータ
struct chip{
int id, s1, s2, s3, s4;
};
struct chip chip_data[] = {
{ 1, 1, 2, 3, 4},
{ 1, 16, 32, 48, 64}, // 2
{ 2, 1, 2, 3, 16},
{ 2, 1, 2, 3, 19},
{ 2, 1, 16, 32, 48},
{ 2, 1, 17, 33, 49},
{ 2, 16, 32, 48, 47},
{ 2, 16, 32, 48, 49},
{ 2, 16, 15, 14, 13},
{ 2, 16, 17, 18, 19}, // 10
{ 3, 1, 2, 3, 17},
{ 3, 1, 2, 3, 18},
{ 3, 16, 15, 32, 48},
{ 3, 16, 17, 32, 48},
{ 3, 16, 32, 31, 48},
{ 3, 16, 32, 33, 48},
{ 3, 16, 17, 15, 14},
{ 3, 16, 15, 17, 18}, // 18
{ 4, 1, 2, 17, 33},
{ 4, 16, 15, 14, 32},
{ 4, 16, 17, 18, 32},
{ 4, 16, 32, 31, 33}, // 22
{ 5, 1, 2, 16, 18},
{ 5, 1, 16, 32, 33},
{ 5, 1, 17, 33, 32},
{ 5, 16, 17, 18, 2}, // 26
{ 6, 16, 15, 17, 32}, // 27
{ 7, 1, 17, 18, 34},
{ 7, 1, 16, 15, 31},
{ 7, 16, 15, 31, 30},
{ 7, 16, 17, 33, 34}, // 31
{ 8, 1, 17, 18, 19},
{ 8, 1, 16, 15, 14},
{ 8, 16, 15, 31, 47},
{ 8, 16, 17, 33, 49},
{ 8, 16, 32, 31, 47},
{ 8, 16, 32, 33, 49},
{ 8, 1, 2, 16, 15},
{ 8, 1, 2, 18, 19}, // 39
{ 9, 1, 16, 15, 32},
{ 9, 1, 17, 18, 33},
{ 9, 16, 15, 17, 33},
{ 9, 16, 17, 15, 31},
{ 9, 16, 15, 14, 31},
{ 9, 16, 17, 18, 33},
{ 9, 16, 17, 32, 31},
{ 9, 16, 15, 32, 33}, // 47
{10, 16, 32, 31, 30},
{10, 1, 2, 16, 32},
{10, 1, 2, 18, 34},
{10, 16, 32, 33, 34}, // 51
{11, 1, 16, 32, 31},
{11, 1, 17, 33, 34},
{11, 16, 15, 14, 30},
{11, 16, 17, 18, 34}, // 55
{12, 16, 15, 32, 31},
{12, 16, 17, 32, 33},
{12, 1, 16, 17, 32},
{12, 1, 16, 17, 33},
{12, 1, 16, 17, 15},
{12, 1, 16, 17, 18},
{12, 1, 2, 16, 17},
{12, 1, 2, 17, 18} // 63
};
class Puzzle{
int cnt;
int cpm;
int board[Bw * Bh];
int chip_use[Fv];
public:
Puzzle();
void disp();
void play(int);
};
Puzzle::Puzzle(){
int i, j;
cnt = 0;
cpm = sizeof(chip_data) / sizeof(chip);
// ボードの初期化
for(i = 0; i < Bw * Bh; i++){
board[i] = Fv;
}
for(i =0; i < Sh; i++){
for(j = 0; j < Sw; j++){
board[Sp + i * Bw + j] = 0;
}
}
for(i = 0; i < Fv; i++){
chip_use[i] = 0;
}
}
// パズルの表示
void Puzzle::disp(){
string ppp[] = {
"・", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"A", "B", "C", "┃"
};
printf(" %d 回目 (%5.3f 秒)\n",
cnt++, (float )clock() / CLOCKS_PER_SEC);
cout << "┏━━━━━━┓" << endl;
for(int y = 1; y < 11; y++){
for(int x = 0; x < 8; x++){
cout << ppp[board[y * Bw + x]];
}
cout << endl;
}
cout << "┗━━━━━━┛" << endl;
}
// パズルの探索
void Puzzle::play(int sp){
int idn, p1, p2, p3, p4;
// 探索点の確認
if(board[sp + 1] != 0 && board[sp + Bw] != 0) return;
// if(board[sp + 1] == 0 || board[sp + Bw] == 0){
for(int i = 0; i < cpm; i++){ // チップ数だけ確認
if(chip_use[chip_data[i].id] != 0){
continue; // チップが使用中なら次へ
}
if(board[p1 = sp + chip_data[i].s1] != 0 ||
board[p2 = sp + chip_data[i].s2] != 0 ||
board[p3 = sp + chip_data[i].s3] != 0 ||
board[p4 = sp + chip_data[i].s4] != 0){
continue; // チップが置けないなら次へ
}
// チップの配置
idn = chip_data[i].id;
board[sp] = idn;
board[p1] = idn;
board[p2] = idn;
board[p3] = idn;
board[p4] = idn;
chip_use[idn] = 1;
// 次の空きを探す
int np = sp;
while(np++ < Mp){
if(board[np] == 0){
break;
}
}
if(np <= Mp){
play(np); // 次のチップを探索
}
else{
disp(); // 解を表示
}
// チップの除去
board[sp] = 0;
board[p1] = 0;
board[p2] = 0;
board[p3] = 0;
board[p4] = 0;
chip_use[idn] = 0;
}
// }
}
// メイン・ルーチン
int main(){
Puzzle pzl;
pzl.disp();
pzl.play(Sp);
}
─────
ペントミノ(pentominop)の改良版 [C++]
// pentomino_plus
//
// 2012.03.18 ───── coded by 心如
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
const Bw = 16; // Board-Width
const Bh = 10; // Board-Height
const Sw = 8; // Space-Width
const Sh = 8; // Space-Height
const Sp = Bw + 1; // Start-Point
const Mp = Sh * Bw + Sw;
const Fv = 14; // frame_value
//チップパターンのデータ
struct chip{
int id, s1, s2, s3, s4;
};
struct chip chip_data[] = {
{ 1, 1, 2, 3, 4},
{ 1, 16, 32, 48, 64},
{ 2, 1, 2, 3, 16},
{ 2, 1, 2, 3, 19},
{ 2, 1, 16, 32, 48},
{ 2, 1, 17, 33, 49},
{ 2, 16, 32, 48, 47},
{ 2, 16, 32, 48, 49},
{ 2, 16, 15, 14, 13},
{ 2, 16, 17, 18, 19},
{ 3, 1, 2, 3, 17},
{ 3, 1, 2, 3, 18},
{ 3, 16, 15, 32, 48},
{ 3, 16, 17, 32, 48},
{ 3, 16, 32, 31, 48},
{ 3, 16, 32, 33, 48},
{ 3, 16, 17, 15, 14},
{ 3, 16, 15, 17, 18},
{ 4, 1, 2, 17, 33},
{ 4, 16, 15, 14, 32},
{ 4, 16, 17, 18, 32},
{ 4, 16, 32, 31, 33},
{ 5, 1, 2, 16, 18},
{ 5, 1, 16, 32, 33},
{ 5, 1, 17, 33, 32},
{ 5, 16, 17, 18, 2},
{ 6, 16, 15, 17, 32},
{ 7, 1, 17, 18, 34},
{ 7, 1, 16, 15, 31},
{ 7, 16, 15, 31, 30},
{ 7, 16, 17, 33, 34},
{ 8, 1, 17, 18, 19},
{ 8, 1, 16, 15, 14},
{ 8, 16, 15, 31, 47},
{ 8, 16, 17, 33, 49},
{ 8, 16, 32, 31, 47},
{ 8, 16, 32, 33, 49},
{ 8, 1, 2, 16, 15},
{ 8, 1, 2, 18, 19},
{ 9, 1, 16, 15, 32},
{ 9, 1, 17, 18, 33},
{ 9, 16, 15, 17, 33},
{ 9, 16, 17, 15, 31},
{ 9, 16, 15, 14, 31},
{ 9, 16, 17, 18, 33},
{ 9, 16, 17, 32, 31},
{ 9, 16, 15, 32, 33},
{10, 16, 32, 31, 30},
{10, 1, 2, 16, 32},
{10, 1, 2, 18, 34},
{10, 16, 32, 33, 34},
{11, 1, 16, 32, 31},
{11, 1, 17, 33, 34},
{11, 16, 15, 14, 30},
{11, 16, 17, 18, 34},
{12, 16, 15, 32, 31},
{12, 16, 17, 32, 33},
{12, 1, 16, 17, 32},
{12, 1, 16, 17, 33},
{12, 1, 16, 17, 15},
{12, 1, 16, 17, 18},
{12, 1, 2, 16, 17},
{12, 1, 2, 17, 18},
{13, 0, 1, 16, 17}
};
class Puzzle{
int cnt; // 解のカウンター
int cpm; // チップパターンの数
int board[Bw * Bh]; // ボード
int chip_use[Fv]; // チップの使用フラグ
public:
Puzzle();
void disp();
void play(int);
};
Puzzle::Puzzle(){
int i, j;
cnt = 0; // 解の数をクリア
cpm = sizeof(chip_data) / sizeof(chip);
// ボードの初期化
for(i = 0; i < Bw * Bh; i++){
board[i] = Fv;
}
for(i =0; i < Sh; i++){
for(j = 0; j < Sw; j++){
board[Sp + i * Bw + j] = 0;
}
}
// チップの使用フラグをクリア
for(i = 0; i < Fv; i++){
chip_use[i] = 0;
}
}
// パズルの表示
void Puzzle::disp(){
string ppp[] = {
"・", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"A", "B", "C", "D", "┃"
};
printf(" %d 回目( %5.3f 秒)\n",
cnt++, (float )clock() / CLOCKS_PER_SEC);
cout << "┏━━━━━━━━┓" << endl;
for(int y = 1; y < 9; y++){
for(int x = 0; x < 10; x++){
cout << ppp[board[y * Bw + x]];
}
cout << endl;
}
cout << "┗━━━━━━━━┛" << endl;
}
// パズルの探索
void Puzzle::play(int sp){
int idn, p1, p2, p3, p4;
// 探索場所の確認
if(board[sp + 1] != 0 && board[sp + Bw] != 0){
return;
}
for(int i = 0; i < cpm; i++){ // チップ数だけ確認
if(chip_use[chip_data[i].id] != 0){ // チップが使用中か確認
continue;
}
// チップが置けるか?
if( board[p1 = sp + chip_data[i].s1] != 0 ||
board[p2 = sp + chip_data[i].s2] != 0 ||
board[p3 = sp + chip_data[i].s3] != 0 ||
board[p4 = sp + chip_data[i].s4] != 0){
continue;
}
// チップの配置
idn = chip_data[i].id;
board[sp] = idn;
board[p1] = idn;
board[p2] = idn;
board[p3] = idn;
board[p4] = idn;
chip_use[idn] = 1;
// 次の空きを探す
int np = sp;
while(np++ < Mp){
if(board[np] == 0){
break;
}
}
if(np <= Mp){
play(np); // 次のチップを探索
}
else{
disp(); // 解を表示
}
// チップの除去
board[sp] = 0;
board[p1] = 0;
board[p2] = 0;
board[p3] = 0;
board[p4] = 0;
chip_use[idn] = 0;
}
}
// メイン・ルーチン
int main(){
Puzzle pzl;
pzl.disp();
pzl.play(Sp);
}
─────
6174は魔法の数!?(実践編) [C++]
過去記事[6174は魔法の数!?]で、「6174」という数は不思議な数であることを紹介しました。
C++を使って、4桁とも同じ数字でない場合は、結果がすべて「6174」になることを確認してみましたので、プログラムを紹介します。
─────
// カプレカー操作
#include <iostream>
using namespace std;
const int Kn = 4; // 桁数
const int Nm = 10000;
// 4桁の数を、4個の数字に分解する関数
void mkary(int x, int* p){
for(int j = 0; j < Kn; j++){
*p++ = x % 10;
x /= 10;
}
}
// 数字を小さい順にならべかえる関数
void sort_arry(int* p, int nm){
int i, j, tmp;
for(i = 0; i < nm - 1; i++){
for(j = i + 1; j < nm; j++){
if(p[i] > p[j]){
tmp = p[i];
p[i] = p[j];
p[j] = tmp;
}
}
}
}
int main(){
int a[Kn], b[Nm];
int i, j, x, cnt, max, min, sbn;
for(i = 0; i < Nm; i++){
cnt = 0;
x = i;
while(1){
mkary(x, a);
sort_arry(a, Kn);
max = a[3] * 1000 + a[2] * 100 + a[1] * 10 + a[0];
min = a[0] * 1000 + a[1] * 100 + a[2] * 10 + a[3];
sbn = max - min;
printf("%d> %04d : %04d - %04d = %04d\n", ++cnt, x, max, min, sbn);
if(x == sbn) break;
x = sbn;
}
b[i] = sbn;
}
for(i = 0; i < Nm; i++){
if(b[i] != 6174) printf("%04d: %04d\n", i, b[i]);
}
}
─────
wpm2.cpp [C++]
// WONDER PUZZLE 02 MAIN_2
//
// 2012.03.21 ───── coded by 心如
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
const Bw = 16; // Board-Width
const Bh = 9; // Board-Height
const Sw = 7; // Board-Space-Width
const Sh = 7; // Board-Space-Height
const Sp = 17; // Start-Point
const Mp = 119; // Maximum-Point
const Fv = 12; // frame-value
// Initialize piece data
struct chip {
int id, s1, s2, s3, s4;
};
struct chip chip_data[] = {
{ 1, 16, 17, 32, 0},
{ 1, 16, 15, 32, 0},
{ 2, 1, 2, 17, 0},
{ 2, 16, 15, 17, 0},
{ 3, 1, 2, 16, 18},
{ 3, 16, 17, 18, 2},
{ 4, 16, 15, 14, 0},
{ 4, 16, 17, 18, 0},
{ 4, 1, 2, 18, 0},
{ 4, 1, 2, 16, 0},
{ 5, 16, 17, 33, 0},
{ 5, 16, 15, 31, 0},
{ 6, 16, 15, 14, 17},
{ 6, 16, 15, 17, 18},
{ 6, 1, 2, 3, 17},
{ 6, 1, 2, 3, 18},
{ 7, 1, 17, 18, 0},
{ 7, 1, 16, 15, 0},
{ 8, 16, 32, 33, 0},
{ 8, 16, 32, 31, 0},
{ 8, 1, 16, 32, 0},
{ 8, 1, 17, 33, 0},
{ 9, 1, 2, 3, 16},
{ 9, 1, 2, 3, 19},
{ 9, 16, 17, 18, 19},
{ 9, 16, 15, 14, 13},
{10, 1, 17, 18, 19},
{10, 1, 16, 15, 14},
{10, 1, 2, 18, 19},
{10, 1, 2, 16, 15},
{11, 1, 2, 17, 18},
{11, 1, 2, 16, 17},
{11, 1, 16, 17, 18},
{11, 1, 16, 17, 15}
};
class Puzzle{
int cnt;
int cpm;
int board[Bw * Bh];
int chip_use[Fv];
public:
Puzzle();
void disp();
void play(int);
};
Puzzle::Puzzle(){
int i, j;
cpm = sizeof(chip_data) / sizeof(chip);
cnt = 0; // 解の数をクリア
// ボードの初期化
for( i = 0; i < Bw * Bh; i++){
board[i] = Fv;
}
for( i =0; i < Sh; i++){
for( j = 0; j < Sw; j++){
board[Sp + i * Bw + j] = 0;
}
}
// チップの使用フラグをクリア
for( i = 0; i < Fv; i++){
chip_use[i] = 0;
}
}
// パズルの表示
void Puzzle::disp(){
int i, j;
string ppp[] = {
"・",
"あ", "い", "う", "え", "お",
"か", "き", "く", "け", "こ",
"さ", "┃"
};
printf(" %d 回目( %5.3f 秒)\n", cnt++, (float )clock() / CLOCKS_PER_SEC);
cout << "┏━━━━━━━┓" << endl;
for(i = 1; i < 8; i++){
for(j = 0; j < 9; j++){
cout << ppp[ board[i * Bw + j]];
}
cout << endl;
}
cout << "┗━━━━━━━┛" << endl;
}
// パズルの探索
void Puzzle::play( int sp){
int idn, p1, p2, p3, p4;
// 探索場所の確認
if( board[sp + 1] != 0 && board[sp + Bw] != 0){
return;
}
// チップ数だけ確認
for(int i = 0; i < cpm; i++){
// チップが使用中か確認
if(chip_use[idn = chip_data[i].id] != 0){
continue;
}
// チップが置けるか?
if(board[p1 = sp + chip_data[i].s1] != 0) continue;
if(board[p2 = sp + chip_data[i].s2] != 0) continue;
if(board[p3 = sp + chip_data[i].s3] != 0) continue;
if(board[p4 = sp + chip_data[i].s4] != 0) continue;
// チップの配置
board[sp] = idn;
board[p1] = idn;
board[p2] = idn;
board[p3] = idn;
board[p4] = idn;
chip_use[idn] = 1;
// 次の空きを探す
int np = sp;
while(np++ < Mp){
if(board[np] == 0) break;
}
if(np <= Mp){
play( np); // 次のチップを探索
}
else{
disp(); // 解を表示
}
// チップの除去
board[sp] = 0;
board[p1] = 0;
board[p2] = 0;
board[p3] = 0;
board[p4] = 0;
chip_use[idn] = 0;
}
}
int main(){
Puzzle pzl;
pzl.disp();
pzl.play(Sp);
}
─────
hexomino.cpp [C++]
// HEXOMINO PUZZLE (PP #600)
//
// 2012.03.15 coded by 心如
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
const Bw = 16; // Board-Width
const Bh = 21; // Board-Height
const Sw = 11; // Space-Width
const Sh = 19; // Space-Height
const Sp = 17; // Start-Point(Bw+1)
const Mp = 315; // Maximum-search-point
const Dp = 172; // Deko-Point
const Fv = 36; // frame-value
// チップパターンのデータ
struct cip {
int id, s1, s2, s3, s4, s5;
};
struct cip chip_data[] = {
{ 1, 1, 2, 3, 4, 5},
{ 1, 16, 32, 48, 64, 80}, // 2
{ 2, 1, 2, 3, 4, 20},
{ 2, 1, 2, 3, 4, 16},
{ 2, 1, 16, 32, 48, 64},
{ 2, 1, 17, 33, 49, 65},
{ 2, 16, 32, 48, 64, 63},
{ 2, 16, 32, 48, 64, 65},
{ 2, 16, 15, 14, 13, 12},
{ 2, 16, 17, 18, 19, 20}, // 10
{ 3, 1, 2, 3, 4, 19},
{ 3, 1, 2, 3, 4, 17},
{ 3, 16, 15, 32, 48, 64},
{ 3, 16, 17, 32, 48, 64},
{ 3, 16, 32, 48, 47, 64},
{ 3, 16, 32, 48, 49, 64},
{ 3, 16, 17, 15, 14, 13},
{ 3, 16, 15, 17, 18, 19}, // 18
{ 4, 1, 2, 3, 4, 18},
{ 4, 16, 15, 14, 17, 18},
{ 4, 16, 32, 31, 48, 64},
{ 4, 16, 32, 33, 48, 64}, // 22
{ 5, 1, 2, 17, 33, 49},
{ 5, 16, 15, 14, 13, 32},
{ 5, 16, 17, 18, 19, 32},
{ 5, 16, 32, 48, 47, 49}, // 26
{ 6, 1, 16, 15, 32, 48},
{ 6, 1, 17, 18, 33, 49},
{ 6, 16, 15, 14, 17, 33},
{ 6, 16, 17, 18, 15, 31},
{ 6, 16, 15, 14, 13, 31},
{ 6, 16, 17, 18, 19, 33},
{ 6, 16, 32, 33, 48, 47},
{ 6, 16, 32, 31, 48, 49}, // 34
{ 7, 1, 16, 32, 31, 48},
{ 7, 1, 17, 33, 34, 49},
{ 7, 16, 17, 15, 14, 30},
{ 7, 16, 15, 17, 18, 34},
{ 7, 16, 15, 14, 13, 30},
{ 7, 16, 17, 18, 19, 34},
{ 7, 16, 15, 32, 48, 49},
{ 7, 16, 17, 32, 48, 47}, // 42
{ 8, 1, 16, 32, 48, 47},
{ 8, 1, 17, 33, 49, 50},
{ 8, 16, 15, 14, 13, 29},
{ 8, 16, 17, 18, 19, 35}, // 46
{ 9, 16, 15, 32, 33, 48},
{ 9, 16, 17, 32, 31, 48},
{ 9, 16, 17, 15, 14, 31},
{ 9, 16, 15, 17, 18, 33}, // 50
{10, 16, 15, 17, 32, 48},
{10, 16, 32, 31, 33, 48},
{10, 16, 15, 17, 18, 32},
{10, 16, 17, 15, 14, 32}, // 54
{11, 1, 2, 3, 16, 18},
{11, 1, 2, 3, 17, 19},
{11, 1, 16, 32, 33, 48},
{11, 1, 17, 33, 32, 49},
{11, 16, 15, 17, 18, 2},
{11, 16, 17, 18, 2, 19},
{11, 16, 15, 32, 48, 47},
{11, 16, 17, 32, 48, 49}, // 62
{12, 1, 2, 3, 19, 20},
{12, 1, 2, 3, 16, 15},
{12, 1, 16, 15, 14, 13},
{12, 1, 17, 18, 19, 20},
{12, 16, 15, 31, 47, 63},
{12, 16, 17, 33, 49, 65},
{12, 16, 32, 48, 47, 63},
{12, 16, 32, 48, 49, 65}, // 70
{13, 1, 2, 3, 18, 34},
{13, 1, 2, 3, 17, 33},
{13, 16, 15, 14, 32, 48},
{13, 16, 17, 18, 32, 48},
{13, 16, 32, 31, 30, 48},
{13, 16, 32, 33, 34, 48},
{13, 16, 32, 31, 33, 34},
{13, 16, 32, 33, 31, 30}, // 78
{14, 1, 2, 18, 19, 20},
{14, 1, 2, 16, 15, 14},
{14, 16, 32, 31, 47, 63},
{14, 16, 32, 33, 49, 65}, // 82
{15, 1, 17, 18, 19, 35},
{15, 1, 16, 15, 14, 30},
{15, 1, 16, 32, 31, 47},
{15, 1, 17, 33, 34, 50},
{15, 16, 15, 14, 30, 29},
{15, 16, 17, 18, 34, 35},
{15, 16, 15, 31, 47, 46},
{15, 16, 17, 33, 49, 50}, // 90
{16, 1, 17, 18, 34, 35},
{16, 1, 16, 15, 31, 30},
{16, 16, 15, 31, 30, 46},
{16, 16, 17, 33, 34, 50}, // 94
{17, 1, 17, 18, 33, 32},
{17, 1, 16, 15, 32, 33},
{17, 16, 17, 18, 2, 33},
{17, 16, 15, 17, 31, 33}, // 98
{18, 1, 17, 18, 19, 34},
{18, 1, 16, 15, 14, 31},
{18, 16, 17, 32, 31, 47},
{18, 16, 15, 32, 33, 49},
{18, 16, 17, 15, 31, 30},
{18, 16, 15, 17, 33, 34},
{18, 16, 15, 31, 30, 47},
{18, 16, 17, 33, 34, 49}, // 106
{19, 1, 17, 18, 19, 33},
{19, 1, 16, 15, 14, 32},
{19, 16, 15, 14, 31, 47},
{19, 16, 17, 18, 33, 49},
{19, 16, 15, 14, 32, 33},
{19, 16, 17, 18, 32, 31},
{19, 16, 32, 31, 33, 47},
{19, 16, 32, 31, 33, 49}, // 114
{20, 1, 2, 17, 33, 32},
{20, 1, 2, 17, 33, 34},
{20, 1, 16, 32, 31, 33},
{20, 1, 17, 33, 32, 34},
{20, 16, 17, 18, 2, 32},
{20, 16, 17, 18, 2, 34},
{20, 16, 17, 18, 32, 34},
{20, 16, 15, 14, 32, 30}, // 122
{21, 1, 2, 16, 18, 19},
{21, 1, 2, 16, 15, 18},
{21, 1, 17, 33, 32, 48},
{21, 1, 16, 32, 33, 49},
{21, 1, 17, 18, 19, 3},
{21, 16, 17, 18, 2, 3},
{21, 16, 15, 31, 47, 48},
{21, 16, 17, 33, 49, 48}, // 130
{22, 1, 2, 18, 19, 34},
{22, 1, 2, 16, 15, 32},
{22, 16, 17, 15, 31, 47},
{22, 16, 15, 17, 33, 49},
{22, 16, 15, 32, 33, 34},
{22, 16, 17, 32, 31, 30},
{22, 16, 32, 31, 30, 47},
{22, 16, 32, 33, 34, 49}, // 138
{23, 1, 2, 18, 19, 35},
{23, 1, 2, 16, 15, 31},
{23, 1, 16, 15, 31, 47},
{23, 1, 17, 18, 34, 50},
{23, 16, 15, 31, 30, 29},
{23, 16, 17, 33, 34, 35},
{23, 16, 32, 31, 47, 46},
{23, 16, 32, 33, 49, 50}, // 146
{24, 1, 2, 18, 34, 35},
{24, 1, 2, 16, 32, 31},
{24, 1, 17, 33, 34, 35},
{24, 1, 16, 32, 31, 30},
{24, 16, 15, 14, 30, 46},
{24, 16, 17, 18, 34, 50},
{24, 16, 32, 31, 30, 46},
{24, 16, 32, 33, 34, 50}, // 154
{25, 1, 2, 18, 34, 33},
{25, 1, 2, 16, 32, 33},
{25, 1, 2, 16, 18, 32},
{25, 1, 2, 16, 18, 34},
{25, 1, 16, 32, 33, 34},
{25, 1, 17, 33, 32, 31},
{25, 16, 32, 31, 30, 14},
{25, 16, 32, 33, 34, 18}, // 162
{26, 1, 17, 18, 33, 34},
{26, 1, 16, 15, 32, 31},
{26, 1, 17, 16, 15, 31},
{26, 1, 16, 17, 18, 34},
{26, 1, 16, 17, 33, 34},
{26, 1, 16, 17, 32, 31},
{26, 16, 15, 14, 31, 30},
{26, 16, 17, 18, 33, 34}, // 170
{27, 1, 2, 17, 18, 33},
{27, 1, 2, 16, 17, 33},
{27, 1, 16, 17, 18, 32},
{27, 1, 16, 15, 17, 33},
{27, 16, 15, 14, 32, 31},
{27, 16, 17, 18, 32, 33},
{27, 16, 15, 31, 32, 33},
{27, 16, 17, 31, 32, 33}, // 178
{28, 1, 16, 15, 17, 32},
{28, 1, 16, 17, 18, 33},
{28, 16, 15, 17, 32, 33},
{28, 16, 15, 17, 32, 31}, // 182
{29, 1, 2, 17, 18, 34},
{29, 1, 2, 16, 17, 32},
{29, 16, 15, 32, 31, 30},
{29, 16, 17, 32, 33, 34}, // 186
{30, 1, 2, 3, 16, 17},
{30, 1, 2, 3, 18, 19},
{30, 1, 16, 17, 15, 14},
{30, 1, 16, 17, 18, 19},
{30, 1, 16, 17, 32, 48},
{30, 1, 16, 17, 33, 49},
{30, 16, 32, 31, 48, 47},
{30, 16, 32, 33, 48, 49}, // 194
{31, 1, 2, 3, 17, 18},
{31, 1, 16, 15, 17, 18},
{31, 16, 15, 32, 31, 48},
{31, 16, 17, 32, 33, 48},
{32, 1, 2, 3, 16, 19},
{32, 1, 16, 32, 48, 49},
{32, 1, 17, 33, 48, 49},
{32, 3, 16, 17, 18, 19},
{33, 1, 2, 17, 18, 19},
{33, 1, 2, 15, 16, 17},
{33, 16, 17, 32, 33, 49},
{33, 15, 16, 31, 32, 47},
{34, 1, 2, 3, 16, 32},
{34, 1, 2, 3, 19, 35},
{34, 1, 2, 16, 32, 48},
{34, 1, 2, 18, 34, 50},
{34, 16, 32, 31, 30, 29},
{34, 16, 32, 33, 34, 35},
{34, 16, 32, 48, 47, 46},
{34, 16, 32, 48, 49, 50},
{35, 1, 2, 16, 17, 18},
{35, 1, 16, 17, 32, 33}
};
class Puzzle{
int cnt;
int board[Bw * Bh];
int chip_use[Fv];
public:
Puzzle();
void disp();
void play(int);
};
Puzzle::Puzzle(){
int i, j;
cnt = 0; // 解の数をクリア
// ボードの初期化
for(i = 0; i < Bw * Bh; i++){
board[i] = Fv;
}
for(i =0; i < Sh; i++){
for(j = 0; j < Sw; j++){
board[Sp + i * Bw + j] = 0;
}
}
board[Dp] = 0;
// チップの使用フラグをクリア
for(i = 0; i < Fv; i++){
chip_use[i] = 0;
}
}
// パズルの表示
void Puzzle::disp(){
int i, j, n = 0;
string ppp[] = {
"・",
"1", "2", "3", "4", "5",
"6", "7", "8", "9", "A",
"B", "C", "D", "E", "F",
"G", "H", "I", "J", "K",
"L", "M", "N", "O", "P",
"Q", "R", "S", "T", "U",
"V", "W", "X", "y", "Z",
"□"
};
printf(" %d 回目 (%5.3f 秒)\n",
cnt++, (float )clock() / CLOCKS_PER_SEC);
for(i = 0; i < Bh; i++){
for(j = 0; j < Bw; j++){
if(j < 14) cout << ppp[ board[n]];
n++;
}
cout << endl;
}
}
// パズルの探索
void Puzzle::play( int sp){
int cpm = sizeof(chip_data) / sizeof(cip); // チップの置き方の数
int i, np, idn, p1, p2, p3, p4, p5;
// 探索場所の確認
if( board[sp + 1] != 0 && board[sp + Bw] != 0) return;
for(i = 0; i < cpm; i++){ // チップ数だけ確認
// チップが使用中か確認
if(chip_use[chip_data[i].id] != 0) continue;
// チップが置けるか確認
if( board[p1 = sp + chip_data[i].s1] != 0 ||
board[p2 = sp + chip_data[i].s2] != 0 ||
board[p3 = sp + chip_data[i].s3] != 0 ||
board[p4 = sp + chip_data[i].s4] != 0 ||
board[p5 = sp + chip_data[i].s5] != 0) continue;
// チップの配置
idn = chip_data[i].id;
board[sp] = idn;
board[p1] = idn;
board[p2] = idn;
board[p3] = idn;
board[p4] = idn;
board[p5] = idn;
chip_use[idn] = 1;
// 次の空きを探す
np = sp;
while(np++ < Mp){
if(board[np] == 0) break;
}
if(np <= Mp){
play(np); // 次の場所を探索
}
else{
disp(); // 解を表示
}
// チップの除去
board[sp] = 0;
board[p1] = 0;
board[p2] = 0;
board[p3] = 0;
board[p4] = 0;
board[p5] = 0;
chip_use[idn] = 0;
}
}
// メイン・ルーチン
int main(){
Puzzle pzl;
pzl.disp();
pzl.play(Sp);
}
─────
hdump.cpp [C++]
// hdump
#include <iostream>
using namespace std;
void hdump(FILE *src, FILE *dst){
int n;
unsigned long count = 0;
unsigned char buf[16];
while((n = fread(buf, 1, 16, src)) > 0){
int i;
fprintf(dst, "%08lX ", count); //アドレス
for(i = 0; i < n; i++){ //十六進
fprintf(dst, "%0*X ", (CHAR_BIT + 3) / 4,(unsigned)buf[i]);
}
if(n < 16){
for(i = n; i < 16; i++){
fputs(" ", dst);
}
}
for(i = 0; i < n; i++){
fputc(isprint(buf[i]) ? buf[i] : '・', dst);
}
fputc('\n', dst);
count += 16;
}
fputc('\n', dst);
}
int main(int argc, char *argv[]){
FILE *fp;
if(argc < 2){
hdump(stdin, stdout);
}
else{
while(--argc > 0){
if((fp = fopen(*++argv, "rb")) == NULL){
fprintf(stderr, "ファイル%sがオープンできません。\n", *argv);
return(1);
}
else{
hdump(fp, stdout); //ファイルから標準出力へ
fclose(fp);
}
}
}
return(0);
}
─────
entab.cpp [C++]
// entab
#include <iostream>
using namespace std;
void entab(FILE *src, FILE *dst, int width){
int ch;
int count = 0;
int ntab = 0;
int pos = 1;
for( ;(ch = fgetc(src)) != EOF; pos++){
if(ch == ' '){
if(pos % width != 0){
count++;
}
else{
count = 0;
ntab++;
}
}
else{
for( ; ntab > 0; --ntab){
fputc('\t', dst);
}
if(ch == '\t'){
count = 0;
}
else{
for( ; count > 0; count--){
fputc(' ', dst);
}
}
fputc(ch, dst);
if(ch == '\n'){
pos = 0;
}
else if(ch == '\t'){
pos += width - (pos - 1) % width - 1;
}
}
}
}
int main(int argc, char *argv[]){
int width = 8;
FILE *fp;
if(argc < 2){
entab(stdin, stdout, width); //標準入力から標準出力へ
}
else{
while(--argc > 0){
if(**(++argv) == '-'){
if(*++(*argv) == 't'){
width = atoi(++*argv);
}
else{
fputs("パラメータが不正です。\n", stderr);
return(1);
}
}
else if((fp = fopen(*argv, "r")) == NULL){
fprintf(stderr, "ファイル%sがオープンできません。\n", *argv);
return(1);
}
else{
entab(fp, stdout, width); //ファイルから標準出力へ
fclose(fp);
}
}
}
return(0);
}
─────
detab.cpp [C++]
// detab
#include <iostream>
using namespace std;
void detab(FILE *src, FILE *dst, int width){
int ch, pos = 1;
while((ch = fgetc(src)) != EOF){
int num;
switch(ch){
case '\t': //タブ文字
num = width - (pos - 1) % width;
for( ;num > 0; num--){
fputc(' ', dst);
pos++;
}
break;
case '\n': //改行文字
fputc(ch, dst);
pos = 1;
break;
default: //通常文字
fputc(ch, dst);
pos++;
break;
}
}
}
int main(int argc, char *argv[]){
int width = 8;
FILE *fp;
if(argc < 2){
detab(stdin, stdout, width);
}
else{
while(--argc > 0){
if(**(++argv) == '-'){
if(*++(*argv) == 't'){
width = atoi(++*argv);
}
else{
fputs("パラメータが不正です。\n", stderr);
return(1);
}
}
else if((fp = fopen(*argv, "r")) == NULL){
fprintf(stderr, "ファイル%sがオープンできません。\n", *argv);
return(1);
}
else{
detab(fp, stdout, width); //ファイルから標準出力へ
fclose(fp);
}
}
}
return(0);
}
─────
random2.cpp [C++]
// Random
//
// 2012.03.03 coded by 心如
#include <iostream>
// #include <ctime>
using namespace std;
// 乱数(0~32767の整数)
class Random{
unsigned long nxt;
public:
Random(){ nxt = 1; }
Random(int sv){ nxt = (unsigned long) sv; }
int GetV(){
nxt = nxt * 1103515245 + 12345;
return (int)((nxt / 65536) % 32768);
}
};
// 経過時間
class Timer{
clock_t stime;
public:
Timer(){ stime = clock(); }
float GetTime(){
return (float )(clock() - stime) / CLOCKS_PER_SEC;
}
};
const Mxn = 200;
const Kai = 1000000;
int main(){
int i, n, max, min;
float fv;
int da[Mxn] = { 0};
Random a;
Timer st;
for( i = 0; i < Mxn * Kai; i++){
da[ a.GetV() % Mxn] += 1;
}
max = Kai;
min = Kai;
for(i = 0; i < Mxn; i++){
printf("%7d,", n = da[ i]);
if( max < n){
max = n;
}
else if( min > n){
min = n;
}
}
cout << endl << "Max=" << max << " Min="<< min << endl;
fv = st.GetTime();
cout << fv << "秒 "<< Mxn * Kai / fv << "回/秒";
}
─────
ペントミノ(pentomino.cpp、改) [C++]
// pentomino
//
// 2012.03.03 coded by 心如
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
const Bw = 16; // Board-Width
const Bh = 12; // Board-Height
const Sw = 6; // Space-Width
const Sh = 10; // Space-Height
const Sp = Bw + 1; // Start-Point
const Mp = Sh * Bw + Sw;
const Fv = 13; // frame_value
const Cpm = 63;
//チップパターンのデータ
struct cip{
int id, s1, s2, s3, s4;
};
struct cip cd[] = {
{ 1, 1, 2, 3, 4},
{ 1, 16, 32, 48, 64}, // 2
{ 2, 1, 2, 3, 16},
{ 2, 1, 2, 3, 19},
{ 2, 1, 16, 32, 48},
{ 2, 1, 17, 33, 49},
{ 2, 16, 32, 48, 47},
{ 2, 16, 32, 48, 49},
{ 2, 16, 15, 14, 13},
{ 2, 16, 17, 18, 19}, // 10
{ 3, 1, 2, 3, 17},
{ 3, 1, 2, 3, 18},
{ 3, 16, 15, 32, 48},
{ 3, 16, 17, 32, 48},
{ 3, 16, 32, 31, 48},
{ 3, 16, 32, 33, 48},
{ 3, 16, 17, 15, 14},
{ 3, 16, 15, 17, 18}, // 18
{ 4, 1, 2, 17, 33},
{ 4, 16, 15, 14, 32},
{ 4, 16, 17, 18, 32},
{ 4, 16, 32, 31, 33}, // 22
{ 5, 1, 2, 16, 18},
{ 5, 1, 16, 32, 33},
{ 5, 1, 17, 33, 32},
{ 5, 16, 17, 18, 2}, // 26
{ 6, 16, 15, 17, 32}, // 27
{ 7, 1, 17, 18, 34},
{ 7, 1, 16, 15, 31},
{ 7, 16, 15, 31, 30},
{ 7, 16, 17, 33, 34}, // 31
{ 8, 1, 17, 18, 19},
{ 8, 1, 16, 15, 14},
{ 8, 16, 15, 31, 47},
{ 8, 16, 17, 33, 49},
{ 8, 16, 32, 31, 47},
{ 8, 16, 32, 33, 49},
{ 8, 1, 2, 16, 15},
{ 8, 1, 2, 18, 19}, // 39
{ 9, 1, 16, 15, 32},
{ 9, 1, 17, 18, 33},
{ 9, 16, 15, 17, 33},
{ 9, 16, 17, 15, 31},
{ 9, 16, 15, 14, 31},
{ 9, 16, 17, 18, 33},
{ 9, 16, 17, 32, 31},
{ 9, 16, 15, 32, 33}, // 47
{10, 16, 32, 31, 30},
{10, 1, 2, 16, 32},
{10, 1, 2, 18, 34},
{10, 16, 32, 33, 34}, // 51
{11, 1, 16, 32, 31},
{11, 1, 17, 33, 34},
{11, 16, 15, 14, 30},
{11, 16, 17, 18, 34}, // 55
{12, 16, 15, 32, 31},
{12, 16, 17, 32, 33},
{12, 1, 16, 17, 32},
{12, 1, 16, 17, 33},
{12, 1, 16, 17, 15},
{12, 1, 16, 17, 18},
{12, 1, 2, 16, 17},
{12, 1, 2, 17, 18} // 63
};
class Timer{
clock_t stime;
public:
Timer(){ stime = clock(); }
float GetTime();
};
float Timer::GetTime(){
return (float )(clock() - stime) / CLOCKS_PER_SEC;
}
class Puzzle{
Timer st;
int cnt;
int bd[Bw * Bh];
int puse[Fv];
public:
Puzzle();
void disp();
void play(int);
};
Puzzle::Puzzle(){
cnt = 0;
// ボードの初期化
int i, j;
for( i = 0; i < Bw * Bh; i++) bd[i] = Fv;
for( i =0; i < Sh; i++){
for( j = 0; j < Sw; j++){
bd[Sp + i * Bw + j] = 0;
}
}
for( i = 0; i < Fv; i++) puse[ i] = 0;
}
// パズルの表示
void Puzzle::disp(){
string ppp[] = {
"・",
"あ", "い", "う", "え", "お",
"か", "き", "く", "け", "こ",
"さ", "し", "□"
};
cout << cnt << "回目 ("<< st.GetTime() << "秒)" << endl;
for(int y = 0; y < Bh; y++){
for(int x = 0; x < Sw + 2; x++){
cout << ppp[bd[y * Bw + x]];
}
cout << endl;
}
}
// パズルの探索
void Puzzle::play(int sp){
int idn, p1, p2, p3, p4;
// 探索場所の確認
if( bd[sp + 1] != 0 && bd[sp + Bw] != 0){
return;
}
// チップ数だけ確認
for(int i = 0; i < Cpm; i++){
// チップが使用中か確認
if(puse[idn = cd[i].id] != 0) continue;
// チップが置けるか?
if(bd[p1 = sp + cd[i].s1] != 0) continue;
if(bd[p2 = sp + cd[i].s2] != 0) continue;
if(bd[p3 = sp + cd[i].s3] != 0) continue;
if(bd[p4 = sp + cd[i].s4] != 0) continue;
// チップの配置
bd[sp] = idn;
bd[p1] = idn;
bd[p2] = idn;
bd[p3] = idn;
bd[p4] = idn;
puse[idn] = 1;
// 次の空きを探す
int np = sp;
while(np++ < Mp){
if(bd[np] == 0) break;
}
if(np <= Mp){
play( np); // 次のチップを探索
}
else{
cnt++; // 解のカウント
disp(); // 解を表示
}
// チップの除去
bd[sp] = 0;
bd[p1] = 0;
bd[p2] = 0;
bd[p3] = 0;
bd[p4] = 0;
puse[idn] = 0;
}
}
int main(){
Puzzle pzl;
pzl.disp();
pzl.play( Sp);
}
─────