bool board::network_dfs(int index, int & total, int color) { if (index) { // index >0 表示有棋子 int x = index % array_size; int y = index / array_size; // m_visited[x][y]=1 表示网络里面已经有这个棋子了 if (!judge_border(x, y) || m_visited[x][y])return 0; // if (m_visited[x][y])return 0; // isfind =1 表示已经找到一个成功网络,可以直接返回 bool isfind(0); // (x,y)位置上的棋子是不同色的 if ((m_curr_color[x][y] % 2) != color)return 0; if (color == BLACK) {// 黑棋 // y==1 有重复的棋子在特区, if (total&&y == 1)return 0; //if ((m_curr_color[x][y] % 2) == WHITE)return 0; if (y == board_size) { if (total >= chess_need_to_win - 1) { isfind = 1; } else return 0; } } else { if (total&&x == 1)return 0; // if ((m_curr_color[x][y] % 2) == BLACK)return 0; if (x == board_size) { if (total >= chess_need_to_win - 1) { isfind = 1; } else return 0; } } m_visited[x][y] = 1; total++; m_success.push(index); if (isfind)return 1; for (int i(0); i < 4; ++i) { // 注意判断指针非空, // change by dyy 8 10 //if (m_network[x][y][i].m_pioneer != NULL) isfind = network_dfs(m_network[x][y][i].m_pioneer->m_index, total, color); if (isfind)return 1; // 注意判断指针非空, if (m_network[x][y][i].m_next != NULL) isfind = network_dfs(m_network[x][y][i].m_next->m_index, total, color); if (isfind)return 1; } m_visited[x][y] = 0; total--; m_success.pop(); return 0; } else return 0; }
bool board::judge_success(int color) { //棋子到达数目才可能形成网络 if (who_step >= 2 * chess_need_to_win - 1) { //白棋连通上下 即y->(1,m_n) 黑棋连通左右 x->(1,m_n) // 黑棋起始位置为 x=1 // 白棋起始位置为 Y=1 int total_chess(0);//成功网络上的棋子数 node * temp_head = 0; if (color) {//黑棋,连通 temp_head = m_head[1][1][3]; } else { temp_head = m_head[1][1][1]; } bool isfind = 0; while (temp_head->m_next) { total_chess = 0; memset(m_visited, 0, sizeof(m_visited)); isfind = 0; // 清空之前的成功网络 while (!m_success.empty())m_success.pop(); isfind = network_dfs(temp_head->m_next->m_index, total_chess, color); if (isfind) { return 1; } temp_head = temp_head->m_next; } } return false; }
BNetwork *sisnet2BNetwork(network_t *net) { int i, j; // topological order array_t *dfs = network_dfs(net); BNetwork *bnet = new BNetwork(); for (i = 0; i < array_n(dfs); i++) { // read nodes one by one node_t *node = array_fetch(node_t *, dfs, i); node_t *fanin; BNodeType nodeType; BNode *rootNode; int numFanin = node_num_fanin(node); string oNodeName(node_name(node)); string nodeName = oNodeName; string faninName; char numStr[16]; switch(node_function(node)) { case NODE_PI: //printf("PI "); nodeType = BNODE_PI; break; case NODE_PO: //printf("PO "); nodeType = BNODE_PO; break; case NODE_AND: //printf("AND "); nodeType = BNODE_AND; break; case NODE_OR: //printf("OR "); nodeType = BNODE_OR; break; case NODE_INV: //printf("INV "); nodeType = BNODE_INV; break; case NODE_BUF: //printf("BUF "); nodeType = BNODE_BUF; break; default: //printf("COMPLEX "); nodeType = BNODE_COMPLEX; break; } //printf("%s\n", node_name(node)); rootNode = bnet->newNode(nodeName, oNodeName, nodeType); //printf("fanins: "); // insert all fanins foreach_fanin(node, j, fanin){ input_phase_t phase = POS_UNATE; int polarity; if (nodeType != BNODE_PO) { phase = node_input_phase(node, fanin); } polarity = (phase == POS_UNATE ? 1 : 0); if (nodeType == BNODE_INV){ polarity = (polarity ? 0 : 1); } //printf("%s %d ", node_name(fanin), phase); WireProperty wp; wp.polarity = polarity; if (j == 0){ bnet->addEdge(node_name(fanin), node_name(node), wp); //cout << "add edge " << node_name(fanin) << "->" << node_name(node) << endl; } else if (j >= numFanin - 1) { faninName = node_name(fanin); bnet->addEdge(faninName, nodeName, wp); //cout << "add edge " << faninName << "->" << nodeName << endl; } else { WireProperty iwp; iwp.polarity = 1; faninName = nodeName; snprintf(numStr, 16, "_%d", j); nodeName = oNodeName + numStr; bnet->newNode(nodeName, rootNode, nodeType); bnet->addEdge(nodeName, faninName, iwp); //cout << "add edge " << faninName << "->" << nodeName << endl; faninName = node_name(fanin); bnet->addEdge(faninName, nodeName, wp); //cout << "add edge " << faninName << "->" << nodeName << endl; } } //printf("\n"); }