Re: [討論] 男人應該要有夢想嗎?平凡一生?
※ 引述《NoPTT (貧乏神入侵!)》之銘言:
: ┌─────────────────────────────────────┐
: │ 文章代碼(AID): #1GK6oiUb (joke) [ptt.cc] [kuso] 日本未來館教育動畫 │
: │ 文章網址: http://www.ptt.cc/bbs/joke/M.1347447980.A.7A5.html │
: │ 這一篇文章值 411 Ptt幣 │
: └─────────────────────────────────────┘
: 這才叫做夢想!
: スーパーコンピュータ!
: 不離不棄! 科學少女啊!
推
09/13 16:46,
09/13 16:46
→
09/13 16:46,
09/13 16:46
雖然我沒有要提供公式,
但是我可以把這位大姊姊寫的程式分享給大家。XD
下面是用 C 寫的版本,如果需要 Java 或其他語言的請自己改囉。
(有人真的要丟給超級電腦跑跑看嗎? :p)
#include <stdio.h>
#include <stdlib.h>
/*
* Find the all paths from node 0 to node 8.
* Using stupid tree traversal algorithm. haha :D
*
* 0 --- 1 --- 2
* | | |
* 3 --- 4 --- 5
* | | |
* 6 --- 7 --- 8
*/
#define START_NODE 0
#define END_NODE 8
#define NUMBER_OF_NODE 9
/* if mPath[n][m] == 1. there is a path between n and m */
static int mPath[NUMBER_OF_NODE][NUMBER_OF_NODE] = {
/* 0 1 2 3 4 5 6 7 8 */
/* 0 */ { 0, 1, 0, 1, 0, 0, 0, 0, 0 },
/* 1 */ { 1, 0, 1, 0, 1, 0, 0, 0, 0 },
/* 2 */ { 0, 1, 0, 0, 0, 1, 0, 0, 0 },
/* 3 */ { 1, 0, 0, 0, 1, 0, 1, 0, 0 },
/* 4 */ { 0, 1, 0, 1, 0, 1, 0, 1, 0 },
/* 5 */ { 0, 0, 1, 0, 1, 0, 0, 0, 1 },
/* 6 */ { 0, 0, 0, 1, 0, 0, 0, 1, 0 },
/* 7 */ { 0, 0, 0, 0, 1, 0, 1, 0, 1 },
/* 8 */ { 0, 0, 0, 0, 0, 1, 0, 1, 0 },
};
/* print the workable path */
void printRecord( int *record );
/* recurrsive method to travesal all paths */
void goNextNode( int* record );
int main() {
/* prepare and init the record */
int *record = (int*) malloc( sizeof(int)*NUMBER_OF_NODE ) ;
for ( int i = 0; i < NUMBER_OF_NODE; i ++ )
record[i] = -1;
record[0] = START_NODE; /* start node 0 */
goNextNode( record );
return 0;
}
void printRecord( int *record ) {
for ( int i = 0; i < NUMBER_OF_NODE; i ++ ) {
if ( record[i] >= 0 )
printf( "%d", record[i] );
else
break;
}
printf("\r\n");
return;
}
void goNextNode( int* record ) {
int idx;
int currentNode = -1;
int currentNodeInRecordPosition = -1;
bool isVisited = false;
/* find current node */
for ( idx = 0; idx < NUMBER_OF_NODE; idx ++ ) {
if ( record[idx] >= 0 ) {
currentNode = record[idx];
currentNodeInRecordPosition = idx;
}
else {
break;
}
}
/* check if we reach the final */
if ( currentNode == END_NODE ) {
printRecord( record );
free( record );
return;
}
/* find next available node */
for ( idx = 0; idx < NUMBER_OF_NODE; idx ++ ) {
if ( mPath[currentNode][idx] == 1 ) {
/* find if such node was already visited before */
isVisited = false;
for ( int j = 0; j < currentNodeInRecordPosition; j ++ ) {
if ( record[j] == idx ) {
isVisited = true;
}
}
if ( isVisited )
continue;
/* "idx" is an available node that didn't vivist before */
int *newRecord = (int*)malloc( sizeof(int)*NUMBER_OF_NODE );
for ( int j = 0; j < NUMBER_OF_NODE; j ++ )
newRecord[j] = record[j];
newRecord[currentNodeInRecordPosition+1] = idx;
goNextNode( newRecord );
}
}
free( record );
return;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.57.131.72
推
09/14 10:50, , 1F
09/14 10:50, 1F
推
09/14 11:27, , 2F
09/14 11:27, 2F
→
09/14 11:28, , 3F
09/14 11:28, 3F
這郭有點難在幾行內回答捏,
如果你是正妹我可以到府免費當你的 C/C++/Java 或是 Android/iOS/Win32 家教,
可惜正妹對這些都沒興趣 T_T (笑)
推
09/14 11:32, , 4F
09/14 11:32, 4F
※ 編輯: meowyih 來自: 61.57.131.72 (09/14 18:11)
推
09/15 00:00, , 5F
09/15 00:00, 5F
→
09/15 00:00, , 6F
09/15 00:00, 6F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 10 之 11 篇):
MenTalk 近期熱門文章
4
10
PTT兩性男女區 即時熱門文章
-24
53