代码20140221

#include<stdio.h>
#include<stdlib.h>
int mark[12][12], maze[12][12];
int move[4][2] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };
int m, n;
struct
{
    int x[100], y[100];
    int top;
}path;
int SeekPath(int x, int y);
void main()
{
    int i,j;
    scanf("%d%d", &m, &n);
    for (i = 1; i <= m;i++)
    for (j = 1; j <= n; j++)
        scanf("%d", &maze[i][j]);
    for (i = 0; i <= m + 1; i++)
    {
        maze[i][0] = 1;
        maze[i][n + 1] = 1;
    }
    for (j = 1; j <= n; j++)
    {
        maze[0][j] = 1;
        maze[m + 1][j] = 1;
    }
    for (i = 0; i <= m + 1;i++)
    for (j = 1; j <= n + 1; j++)
        mark[i][j] = 0;
    path.top = 0;
    if (SeekPath(1, 1))
    {
        path.x[path.top] = 1;
        path.y[path.top] = 1;
        path.top++;
    }
    j = 1;
    for (i = path.top - 1; i >= 0; i--)
    {
        printf("(%d,%d)", path.x[i], path.y[i]);
        if ((j++) % 5 == 0)
            printf("\n");
    }
    system("pause");
}
int SeekPath(int x, int y)
{
    int i, g, h;
    mark[x][y] = 1;
    if (x == m&&y == n)
        return(1);
    for (i = 0; i <=4; i++)
    {
        g = x + move[i][0];
        h = y + move[i][1];
        if ((maze[g][h] == 0) && (mark[g][h] == 0))
        {
            mark[g][h] = 1;
            if (SeekPath(g, h))
            {
                path.x[path.top] = g;
                path.y[path.top] = h;
                path.top++;
                return(1);
            }
        }
    }
    return(0);
}
struct
{
    int x, y;
    int px, py;
}que[100],s[100];
int h, t;
int top;

int SeekPath(int x, int y)
{
    int cx, cy, cpx, cpy, dx, dy, i;
    que[t].x = x;
    que[t].y = y;
    que[t].px = 0;
    que[t].py = 0;
    t++;
    s[top].x = x;
    s[top].y = y;
    s[top].px = 0;
    s[top].py = 0;
    top++;
    while (h != t)
    {
        cx = que[h].x;
        cy = que[h].y;
        cpx = que[h].px;
        cpy = que[h].py;
        h++;
        if (cx == m&&cy == n)
        {
            break;
        }
        s[top].x = cx;
        s[top].y = cy;
        s[top].px = cpx;
        s[top].py = cpy;
        top++;
        for (i = 0; i < 4; i++)
        {
            dx = cx + move[i][0];
            dy = cy + move[i][1];
            if (maze[dx][dy] == 0 && mark[dx][dy] == 0)
            {
                mark[dx][dy] = 1;
                que[t].x = dx;
                que[t].y = dy;
                que[t].px = cx;
                que[t].py = cy;
                t++;
            }
        }
    }
    printf("%d,%d\n", cx, cy);
    while (top != 0)
    {
        if (cpy == s[top].x&&cpy == s[top].y)
        {
            printf("%d,%d\n", cpx, cpy);
            cpx = s[top].px;
            cpy = s[top].py;
        }
        top--;
    }
    return(0);
}
#include<stdio.h>
#define M 4
#define N 5
int move[8][2] = { { -1, -2 }, { -1, 2 }, { -2, 1 }, { -2, -1 }, { 2, 1 }, { 2, -1 }, { 1, 2 }, { 1, -2 } };
int mark[M][N] = {
    0, 0, 0, 0, 0,
    0, 0, 0, 0, 0,
    0, 0, 0, 0, 0,
    0, 0, 0, 0, 0
};
int SeekPath(int x, int y);
int sx, sy, num;
int main()
{
    scanf("%d%d", &sx, &sy);
    sx--;
    sy--;
    num = 0;
    mark[sx][sy] = 1;
    SeekPath(sx, sy);
    printf("%d\n", num);
    return 0;
}
int SeekPath(int x, int y)
{
    int i, dx, dy;
    for (i = 0; i < 8; i++)
    {
        dx = x + move[i][0];
        dy = y + move[i][1];
        if (dx >= 0 && dy >= 0 && dx < M&&dy < N&&mark[dx][dy] == 0)
        {
            mark[dx][dy] = 1;
            SeekPath(dx, dy);
            mark[dx][dy] = 0;
        }
        if (dx == sx&&dy == sy)
            num++;
    }
    return(0);
}

 

//
//struct QUETP
//{
//    char que[QN][21];
//    int c[QN];
//    int front, rear;
//};

#include<stdio.h>
#include<string.h>
#define QN 1000
struct QUETP
{
    char que[QN][21];
    int c[QN];
    int front, rear;
}que;
void initqueue(struct QUETP *q)
{
    q->front = 0;
    q->rear = 0;
}
int enqueue(struct QUETP*q, char*str, int m)
{
    if (q->front == (q->rear + 1) % QN)
        return(0);
    strcpy(q->que[q->rear], str);
    q->c[q->rear] = m;
    q->rear = (q->rear + 1) % QN;
    return(1);
}

int delqueue(struct QUETP*q, char*str, int *m)
{
    if (q->front == q->rear)
        return(0);
    strcpy(str, q->que[q->front]);
    *m = q->c[q->front];
    q->front = (q->front + 1) % QN;
    return(1);
}
int emptyqueue(struct QUETP q)
{
    if (q.front == q.rear)
        return(1);
    else
        return(0);
}
int findstr(char*strs, int k, char*substr)
{
    int len, slen, i, j;
    len = strlen(strs);
    slen = strlen(substr);
    for (i = k; i < len - slen + 1; i++)
    {
        for (j = 0; j < slen; j++)
        {
            if (strs[j + i] != substr[j])
                break;
        }
        if (j == slen)
            return(i);
    }
    return(-1);
}
void replstr(char*strs, int pos, char*substr1, char*substr2, char *tarstr)
{
    int len;
    len = strlen(substr1);
    strcpy(tarstr, strs);
    tarstr[pos] = '\0';
    strcat(tarstr, substr2);
    strcat(tarstr, &strs[pos + len]);
}
void main()
{
    struct
    {
        char StrS[21], StrT[21];
    }rule[6];
    char strA[21], strB[21], currstr[200], tarstr[200];
    FILE*fp;
    int n, i, k, count, fg, m;
    fp = fopen("in.txt", "r");
    fscanf(fp, "%d", &m);
    while (m-- > 0)
    {
        fscanf(fp, "%s%s", strA, strB);
        fscanf(fp, "%d", &n);
        for (i = 0; i < n; i++)
        {
            fscanf(fp, "%s%s", rule[i].StrS, rule[i].StrT);
        }
        initqueue(&que);
        if (enqueue(&que, strA, 0) == 0)
            return;
        count = 0; fg = 0;
        while (!emptyqueue(que))
        {
            delqueue(&que, currstr, &count);
            if (strcmp(currstr, strB) == 0)
            {
                fg = 1;
                break;
            }
            if (count > 10)
                continue;
            for (i = 0; i < n; i++)
            {
                k = 0;
                while (k>0)
                {
                    k = findstr(currstr, k, rule[i].StrS);
                    if (k >= 0)
                    {
                        replstr(currstr, k, rule[i].StrS, rule[i].StrT,tarstr);
                        if (strlen(tarstr) <= 20)
                        {
                            if (enqueue(&que, tarstr, count + 1) == 0)
                            {
                                printf("Queue Overflow\n");
                                goto rt;
                            }
                        }
                        k++;
                    }
                }
            }
        }
    rt:
        if (fg == 0)
            printf("NO ANSWER!\n");
        else
            printf("%d\t", count);
    }
    fclose(fp);
    return;
}


#include<stdio.h>
#include<string.h>
struct
{
    char StrS[21], StrT[21];
}rule[6];
char strA[21], strB[21];
int findstr(char*strs, int k, char*substr)
{
    int len, slen, i, j;
    len = strlen(strs);
    slen = strlen(substr);
    for (i = k; i < len - slen + 1; i++)
    {
        for (j = 0; j < slen; j++)
        {
            if (strs[j + i] != substr[j])
                break;
        }
        if (j == slen)
            return(i);
    }
    return(-1);
}
void replstr(char*strs, int pos, char*substr1, char*substr2, char*tarstr)
{
    int len;
    len = strlen(substr1);
    strcpy(tarstr, strs);
    tarstr[pos] = '\0';
    strcat(tarstr, substr2);
    strcat(tarstr, &strs[pos + len]);
}
void tryreplace(char*str, int lev, int*fg, int n)
{
    int k, i;
    char currstr[230], tarstr[230];
    if (*fg > 0)
        return;
    if (strcmp(str, strB) == 0)
    {
        *fg = lev;
        return;
    }
    if (lev >= 10)
        return;
    strcpy(currstr, str);
    for (i = 0; i < n; i++)
    {
        k = 0;
        while (k>0)
        {
            k = findstr(currstr, k, rule[i].StrS);
            if (k < 0)
                break;
            if (k >= 0)
            {
                replstr(currstr, k, rule[i].StrS, rule[i].StrT, tarstr);
                if (strlen(tarstr) <= 0)
                {
                    tryreplace(tarstr, lev + 1, fg, n);
                    if (*fg>0)
                        return;
                }
            }
            k++;
        }
    }
    return;
}
void main()
{
    char currstr[200];
    FILE*fp;
    int n, i, fg, m;
    fp = fopen("in.txt", "r");
    fscanf(fp, "%d", &m);
    while (m-- > 0)
    {
        fscanf(fp, "%d", &n);
        for (i = 0; i < n; i++)
        {
            fscanf(fp, "%s%s", rule[i].StrS, rule[i].StrT);
        }
        strcpy(currstr, strA);
        fg = 0;
        tryreplace(currstr, 0, &fg, n);
        if (fg == 0)
            printf("NO ANSWEER!\n");
        else
            printf("%d\n", fg);
    }
    fclose(fp);
}

#include<stdio.h>
#define MAX_NODE 10000
#define MAX_LEVEL 500
typedef struct nodetp
{
    int digit, res;
    int parent;
}nodetp;
nodetp queue[MAX_NODE];
int tail;
int setx[10], num, existres[MAX_NODE];
void print_res(int n)
{
    int i, k = 0;
    char s[MAX_LEVEL];
    for (i = n; i > 0; i = queue[i].parent)
        s[k + 1] = '0' + queue[i].digit;
    while (k > 0)
        putchar(s[--k]);
    putchar('\n');
}
int trySearch(int hd)
{
    int i, r;
    for (i = (hd == 0); i < 10; i++)
    {
        if (!setx[i])
            continue;
        r = (queue[hd].res * 10 + i) % num;
        if (existres[r])
            continue;
        existres[r] = 1;
        queue[tail].digit = i;
        queue[tail].res = r;
        queue[tail].parent = hd;
        tail++;
        if (r == 0)
        {
            print_res(tail - 1);
            return(1);
        }
    }
    return(0);
}
void main(void)
{
    int i, t, m, head, end, level, fg;
    while (1)
    {
        scanf("%d",&num);
        if (num == 0)
            break;
        for (i = 0; i < 10; i++)
            setx[i] = 0;
        for (i = 0; i < MAX_NODE; i++)
            existres[i] = 0;
        scanf("%d", &m);
        for (i = 0; i < m; i++)
        {
            scanf("%d", &m);
            setx[t] = 1;
        }
        queue[0].digit = queue[0].res = 0;
        queue[0].parent = -1;
        tail = 1;
        fg = 0;
        for (head = level = 0; level < MAX_LEVEL; level++)
        {
            for (end = tail; head < end; head++)
            if (trySearch(head) == 1)
            {
                fg = 1;
                break;
            }
            if (fg = 1)
                break;
        }
        if (fg = 0)
            printf("NOFOUND\n");
    }
}

Boolean visited[MAX];
void DFSTraverse(Graph G)
{
    for (v = 0; v < G.vexnum; ++v)
        visited[v] = FALSE;
    for (v = 0; v < G.vexnum;++v)
    if (!visited[v])
        DFS(G, v);
}
void DFS(Graph G, int v)
{
    visited[v] = TRUE;
    Visited(v);
    for (w = FirstAdjVex(G, v); w;w=NextAdjVex(G,v,w))
    if (!visited[w])
        DFS(G, w);
}

void BFSTraverse(Graph G)
{
    for (v = 0; v < G.vexnum; ++v)
        visited[v] = FALSE;
    InitQueue(Q);
    for (v = 0; v < G.vexnum;++v)
    if (!visited[v])
    {
        visited[v] = TRUE;
        visit(v);
        Enqueue(Q, v);
        while (!QueueEmpty(Q))
        {
            Dequeue(Q, u);
            for (w = FirstAdjVex(G, u); w;w=NextAdjVex(G,u,w))
            if (!visited[w])
            {
                visited[w] = TRUE;
                visit(w);
                EnQueue(Q, w);
            }
        }
    }
}

#include<string.h>
char a[100][100];
int b[100][100], n, m;
int x [] = { 0, -1, -1, -1, 0, 1, 1, 1 };
int y [] = { 1, 1, 0, -1, -1, -1, 0, 1 };
void dfs(int i, int j)
{
    int k;
    int tx, ty;
    b[i][j] = 0; //设置标志
    for (k = 0; k < 8; k++)//对8个方向逐个检查
    {
        tx = i + x[k];//计算新节点的位置
        ty = j + y[k];//节点坐标合法,并没有被访问,连通值为'@'
        if (tx >= 0 && tx < n&&ty >= 0 && ty < m&&b[tx][ty] && a[tx][ty] == '@')
            dfs(tx, ty);//从新节点开始,递归

    }
}

int main()
{
    int i, j, num;
    while (1)
    {
        scanf("%d  %d", &n, &m);
        if (n == 0 && m == 0)//测试数据结束
            break;
        for (i=0; i < n;i++)
        for (j = 0; j < m; j++)
            b[i][j] = 1;//标志数组设初值
        num = 0;//地块个数计数初始化
        for (i = 0; i < n; i++)//读入n行
            scanf("%s", &a[i]);//读入每行m个字符
        for (i = 0; i < n; i++)
        {
            for (j = 0; j < m;j++)//查找有石油的节点
            if (b[i][j] && a[i][j] == '@')
            {
                num++;//计数加1
                dfs(i, j);//从此节点开始搜索,调用算法bfs(i,j)
            }
        }
        printf("%d\n", num);
    }
    return(0);
}
int queue[10000][2];//增加一个队列
void bfs(int i, int j)
{
    int k, tx, ty, cx, cy;
    int head = 0, tail = 0;//队列头尾指针的初值
    queue[tail][0] = i;//当前位置进入队列
    queue[tail][1] = j;
    tail++;//队列指针加1
    b[i][j] = 0;//设置标志
    while (head < tail)//队列不空
    {
        cx = queue[head][0];//出队
        cy = queue[head][1];
        head++;//对头指针加1
        for (k = 0; k < 8; k++)//对8个方向逐个检查,并计算新节点的位置
        {
            tx = cx + x[k];
            ty = cy + y[k];
            if (tx >= 0 && tx < n&&ty >= 0 && ty < m&&b[tx][ty] && a[tx][ty] == '@')//满足条件,进入队列
            {
                queue[tail][0] = tx;
                queue[tail][1] = ty;
                tail++;
                b[tx][ty] = 0;
            }
        }
    }
}

 

posted @ 2014-02-21 10:56  yuanqi  阅读(223)  评论(0编辑  收藏  举报