Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
水题,一个DFS搞定,没什么BT数据这题要是有BT数据,可以有10^10个点,输入都读不完就铁定TLE #include <stdio.h> #include <string.h> #include <iostream> #include <cstdlib> #include <string> #include <vector> #include <map> using namespace std; int cnt; int n; map<long long int, int> idx; int adjNum[10000]; int adj[10000][10]; int qd, zd; int getIdx(){ int Temp; scanf("%d",&Temp); long long int temp = Temp; if(temp == -1) return -1; for(int i = 1; i < n; i++){ int tmp; scanf("%d", &tmp); temp *= 10; temp += tmp; } map<long long int, int>::iterator it = idx.find(temp); if(it != idx.end()) return it->second; idx.insert(pair<long long int, int>(temp, cnt)); cnt ++; return cnt-1; } bool dfs(int q, bool *used){ used[q] = 1; if(q == zd) return 1; for(int i = 0; i < adjNum[q]; i++){ if(used[adj[q][i]]) continue; if(dfs(adj[q][i], used)) return 1; } return 0; } int main(int argc, char **argv){ int ct = 0; while(1){ cin >> n; if(!n) break; ct++; cout << "Maze #" << ct << " can"; cnt = 0; idx.clear(); for(int i = 0; i < 10000; i++) adjNum[i] = 0; qd = getIdx(), zd = getIdx(); int x,y; while((x = getIdx()) != -1){ y = getIdx(); adj[x][adjNum[x]] = y; adjNum[x]++; adj[y][adjNum[y]] = x; adjNum[y]++; } bool used[10000] = {0}; if(!dfs(qd, used)) cout << "not"; cout << " be travelled" << endl; } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator