接上一篇:
- void main79()
- {
- hanoi(3,'a','b','c' );
- printf("%I64d\n",hanoiIII(12));
- }
- char maze2[MAZESIZE][MAZESIZE];
- int go2[][2] =
- {
- {-1,0},
- {-1,1},
- {0,1},
- {1,1},
- {1,0},
- {1,-1},
- {0,-1},
- {-1,-1}
- };
- void deepInto(int i,int j,int m,int n)
- {
- maze2[i][j] = '#';
- for(int k=0; k<8; ++k)
- {
- int ii = i+go2[k][0];
- int jj = j+go2[k][1];
- if(ii <= 0 || ii > m || jj <= 0 || jj > n) continue;
- if(maze2[ii][jj] == '@')
- deepInto(ii,jj,m,n);
- }
- }
- int findPlot(int m,int n)
- {
- int ret = 0;
- for(int i=1; i<=m; ++i)
- for(int j=1; j<=n; ++j)
- {
- if(maze2[i][j] == '@')
- {
- ++ret;
- deepInto(i,j,m,n);
- }
- }
- return ret;
- }
- void main81()
- {
- int n,m;
- while(cin>>m>>n && m)
- {
- for(int i=1; i<=m; ++i)
- for(int j=1; j<=n; ++j)
- cin>>maze2[i][j];
- cout<<findPlot(m,n)<<endl;
- }
- }
- int NN,MM,TT;
- bool success;
- void findTpath(int S1,int E1,int S2,int E2,int t)
- {
- /* 这种方法不行, 出口不能在这判断
- if(S1 == S2 && E1 == E2)
- {
- cout<<"find it "<<t<<endl;
- if(t == TT)
- success = true;
- }
- */
- for(int i=0; i<4; ++i)
- {
- int ii=S1 + dir[i][0];
- int jj=E1 + dir[i][1];
- if(ii < 1 || ii > NN || jj <1 || jj > MM)
- continue;
- if(maze2[ii][jj] == '.')
- {
- maze2[ii][jj] = '#';
- findTpath(ii,jj,S2,E2,t+1);
- maze2[ii][jj] = '.';
- }
- if(maze2[ii][jj] == 'D') //查找出口的时候在这判断
- {
- if(t+1 == TT)
- success = true;
- return;
- }
- }
- }
- void main80()
- {
- int S1,S2,E1,E2;
- while(cin>>NN>>MM>>TT && NN)
- {
- for(int i=1; i<=NN; ++i)
- for(int j=1; j<=MM; ++j)
- {
- cin>>maze2[i][j];
- if(maze2[i][j] == 'S') { S1 = i; E1 = j; }
- else if(maze2[i][j] == 'D'){ S2 = i; E2 = j; }
- }
- maze2[S1][E1] = '#';
- success = false;
- findTpath(S1,E1,S2,E2,0);
- if(success)
- cout<<"yes"<<endl;
- else cout<<"no"<<endl;
- }
- }
- void main82()
- {
- __int64 F[91];
- F[1] = 1;
- F[2] = 2;
- for(int i=3; i<91; ++i)
- F[i] = F[i-1] + F[i-2];
- while(cin>>i)
- printf("%I64d\n",F[i]);
- }
- int dp[N];
- int l[N];
- int LIS(int m)
- {
- int ret = 1;
- for(int i=1; i<m; ++i)
- for(int j=i-1; j>=0; --j)
- {
- if(l[i] <= l[j] && dp[i] < dp[j]+1) //此题中高度相等也要计算
- {
- dp[i] = dp[j] + 1;
- }
- }
- for(i=0; i<m; ++i)
- {
- if(ret < dp[i])
- ret = dp[i];
- }
- return ret;
- }
- void main84()
- {
- int m,i;
- cin>>m;
- for(i=0; i<m; ++i)
- {
- dp[i] = 1;
- cin>>l[i];
- }
- cout<<LIS(m)<<endl;
- }
- #define max(x,y) (x>y?x:y)
- void main85()
- {
- char s1[100],s2[100];
- int dp[100][100];
- cin>>s1>>s2;
- int l1 = strlen(s1);
- int l2 = strlen(s2);
- for(int i=0; i<=l1; ++i) dp[i][0] = 0;
- for(int j=0; j<=l2; ++j) dp[0][j] = 0;
- for(i=1; i<=l1; ++i)
- {
- for(j=1; j<=l2; ++j)
- {
- if(s1[i-1] == s2[j-1]) //字符串从0开始遍历
- dp[i][j] = dp[i-1][j-1]+1; //此处一定要有加一操作 //对dp数组的赋值从1开始的,位置0的已经赋值为0
- else
- {
- dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
- }
- }
- }
- int ret = 0;
- for(i=1; i<=l1; ++i)
- for(j=1; j<=l2; ++j)
- if(ret < dp[i][j])
- ret = dp[i][j];
- cout<<ret<<endl;
- }
- void main86()
- {
- stack<double> si;
- stack<char> sc;
- double n;
- char ch;
- char str[201];
- string s;
- while(gets(str))
- {
- s = str;
- istringstream iss(s);
- while(!si.empty()) si.pop();
- while(!sc.empty()) sc.pop();
- int i=0;
- while(!iss.eof())
- {
- iss>>n;
- si.push(n);
- if(!iss.eof())
- {
- iss>>ch;
- // cout<<++i<<" "<<n<<" "<<ch<<endl;
- if(!sc.empty())
- {
- char tmp = sc.top();
- if(ch == '+' || ch == '-')
- {
- sc.pop();
- double itmp = si.top();
- si.pop();
- double itmp2 = si.top();
- si.pop();
- switch(tmp)
- {
- case '+':
- si.push(itmp+itmp2);
- break;
- case '-':
- si.push(itmp2 - itmp);
- break;
- case '*':
- si.push(itmp * itmp2);
- break;
- case '/':
- si.push(itmp2 / itmp);
- break;
- }
- sc.push(ch);
- }else //当前取到的是*,/
- {
- double itmp ;
- double itmp2;
- switch(tmp)
- {
- case '*':
- sc.pop();
- itmp = si.top();
- si.pop();
- itmp2 = si.top();
- si.pop();
- si.push(itmp * itmp2); // cout<<itmp * itmp2<<endl;
- sc.push(ch);
- break;
- case '/':
- sc.pop();
- itmp = si.top();
- si.pop();
- itmp2 = si.top();
- si.pop();
- si.push(itmp2 / itmp); // cout<<itmp2/ itmp<<endl;
- sc.push(ch);
- break;
- default:
- sc.push(ch);
- break;
- }
- }
- }else
- sc.push(ch);
- }
- }
- while(!sc.empty())
- {
- char tmp = sc.top();
- sc.pop();
- double itmp = si.top();
- si.pop();
- double itmp2 = si.top();
- si.pop();
- switch(tmp)
- {
- case '+':
- si.push(itmp + itmp2);
- break;
- case '-':
- si.push(itmp2 - itmp);
- break;
- case '*':
- si.push(itmp*itmp2);
- break;
- case '/':
- si.push(itmp2 / itmp);
- break;
- }
- }
- cout<<fixed<<setprecision(2)<<si.top()<<endl;
- si.pop();
- }
- }
- void main87()
- {
- int n;
- cin>>n;
- priority_queue<int,vector<int>,greater<int> > pq;
- while(n--)
- {
- int tmp;
- cin>>tmp;
- pq.push(tmp);
- }
- int ret = 0;
- while(pq.size() > 1) //注意此处求哈夫曼树时候,队列中的元素不能全部出去
- {
- int itmp = pq.top();
- pq.pop();
- int itmp2 = pq.top();
- pq.pop();
- ret += (itmp + itmp2);
- pq.push(itmp+itmp2);
- }
- cout<<ret<<endl;
- }
- void main88()
- {
- vector<string> vs;
- vs.push_back("Abb");
- vs.push_back("aab");
- sort(vs.begin(),vs.end());
- copy(vs.begin(),vs.end(),ostream_iterator<string>(cout," "));
- }
- typedef struct node
- {
- node *lchild, *rchild;
- int data;
- }BTree2;
- void insert(BTree2 * &root,int data)
- {
- if(root == NULL)
- {
- root = (BTree2 *)malloc(sizeof(BTree2));
- root->data = data;
- root->lchild = root->rchild = NULL;
- }else if(root->data < data)
- insert(root->rchild,data);
- else insert(root->lchild,data);
- }
- void postOrder3(BTree2 *root,vector<int> &v)
- {
- stack<BTree2 *> sb;
- stack<bool> sbb;
- BTree2 *p;
- bool flag;
- if(root)
- {
- p = root;
- while(p != NULL || !sb.empty())
- {
- while(p != NULL)
- {
- sb.push(p);
- p = p->lchild;
- sbb.push(false);
- }
- p = sb.top();
- sb.pop();
- flag = sbb.top();
- sbb.pop();
- if(flag)
- {
- cout<<p->data<<" ";
- v.push_back(p->data);
- p = NULL;
- }else
- {
- sb.push(p);
- sbb.push(true);
- p = p->rchild;
- }
- }
- }
- }
- void deleteBTree(BTree2 *root)
- {
- if(root)
- {
- deleteBTree(root->lchild);
- deleteBTree(root->rchild);
- free(root);
- }
- }
- void main89()
- {
- int n;
- cin>>n;
- BTree2 *root = NULL;
- while(n--)
- {
- int tmp;
- cin>>tmp;
- insert(root,tmp);
- }
- vector<int> v;
- postOrder3(root,v);
- copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
- deleteBTree(root);
- }