Uva--196 (拓扑排序*)
2014-07-09 21:19:01
题意&思路:模拟电子表格。拓扑排序(搜索)。。。。数字判断:'0' -- '9'(被坑了一个下午,QAQ,不忍直视自己的智商)
1 #include <cstdio> 2 #include <iostream> 3 #include <stack> 4 #include <vector> 5 #include <cstring> 6 using namespace std; 7 int Case,row,col; 8 struct node{ 9 int r,c,val; //row,column,value 10 int inc,num; //入度, 11 vector<int> tr,tc; 12 }; 13 node g[1000][1000]; 14 void Dfs(){ 15 stack<node> s; 16 for(int i = 1; i <= row; ++i) 17 for(int j = 1; j <= col; ++j) 18 if(g[i][j].inc == 0) 19 s.push(g[i][j]); 20 while(!s.empty()){ 21 node a = s.top(); 22 s.pop(); 23 for(int i = 0; i < a.num; ++i) if(a.tr[i] && a.tc[i]){ 24 g[a.tr[i]][a.tc[i]].val += a.val; 25 //printf("%d %d : v:%d,%d\n",a.tr[i],a.tc[i],a.val,g[a.tr[i]][a.tc[i]].val); 26 g[a.tr[i]][a.tc[i]].inc--; 27 if(g[a.tr[i]][a.tc[i]].inc == 0) 28 s.push(g[a.tr[i]][a.tc[i]]); 29 a.tr[i] = a.tc[i] = 0; 30 } 31 } 32 } 33 int main(){ 34 scanf("%d",&Case); 35 while(Case--){ 36 memset(g,0,sizeof(g)); 37 char str[300]; 38 scanf("%d %d",&col,&row); 39 for(int i = 1; i <= row; ++i){ 40 for(int j = 1; j <= col; ++j){ 41 scanf("%s",str); 42 if(str[0] != '=') 43 sscanf(str,"%d",&g[i][j].val); 44 else{ 45 int numr = 0,numc = 0,len = strlen(str); 46 for(int k = 1; k <= len; ++k){ 47 if(str[k] >= 'A' && str[k] <= 'Z') //letter 48 numc = numc * 26 + (int)(str[k] - 'A' + 1); 49 else if(str[k] >= '0' && str[k] <= '9') //integer 50 numr = numr * 10 + (int)(str[k] - '0'); 51 else{ 52 g[i][j].inc++; //g[i][j]入度加一 53 g[numr][numc].tr.push_back(i); 54 g[numr][numc].tc.push_back(j); 55 //printf("i:%d j:%d\n",g[numr][numc].tr.back(),g[numr][numc].tc.back()); 56 g[numr][numc].num++; 57 //使公式所需的节点保存该点坐标,num相当于出度 58 numr = 0; //init 59 numc = 0; 60 } 61 } 62 } 63 } 64 } 65 Dfs(); 66 for(int i = 1; i <= row; ++i){ 67 printf("%d",g[i][1].val); 68 for(int j = 2; j <= col; ++j) 69 printf(" %d",g[i][j].val); 70 puts(""); 71 } 72 } 73 return 0; 74 }