Hdu 1571 下沙小面的(1) [模拟]

来源:互联网 发布:社区巡展 知乎 编辑:程序博客网 时间:2024/06/05 22:02

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1571

题目的意思很是简单,就是模拟一个面的的情况。直接链表模拟就可以了。。。

表示最近写了一些模拟,才发现自己的代码能力的羸弱。。。。

写了一些个链表的模拟,一直各种Bug,RE,WA。。反正是各种错误的有。。。

反正,还是多写吧。。自己的代码能力还是很弱。。。。

这只能用多写才能够解决的问题。。。

Code:

#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <queue>using namespace std;const int N = 33;struct Node{    int num;    Node *next;    Node(){        next = NULL;    }    Node(int x) {        num = x;        next = NULL;    }} *root, *tail;int size(){    Node *p = root;    int ans = 0;    while(p -> next != tail){        p = p -> next;        ans ++;    }    return ans;}int Delete(int x){    int ans = x;    root = root ->next;    root -> num = -1;    Node *p = root;    while(p -> next != NULL){        if(p -> next -> num == ans){            p ->next = p ->next ->next;        }        p = p -> next;    }    return ans;}int main(){//    freopen("1.txt", "r", stdin);    int n;    while(scanf("%d", &n) && n){        int map[N][N];        for(int i = 0; i < n; i ++){            for(int j = 0; j < n; j ++)            scanf("%d", &map[i][j]);        }        int m, now = 0, x, ans = 0;        char order[5];        root = new Node(-1);// define the root and tail.. and tail is the next node of root.        tail = new Node(-1);        root -> next = tail;        scanf("%d", &m);        while(m --){            scanf("%s", order);            if(order[0] == 'U'){                scanf("%d", &x);                if(size() == 7 || now == x){                    continue;                }                else {                    tail -> num = x;                    tail -> next = new Node(-1);                    tail = tail -> next;                }            }            else {                if(root -> next== tail) continue;                int to = Delete(root -> next -> num);                ans += map[now][to];                now = to;            }        }        printf("%d\n", ans);        delete root;        delete tail;    }    return 0;}


0 0
原创粉丝点击