uva--232(字符串模拟)

点击打开链接

这是一道字符串模拟题,题意大概是给定一个m*n的网格,黑格用‘*’表示,白格有一个字母,如果一个白格左边或者上面没有黑格子,则称为一个起始格。

然后找出所有横向单词和竖向单词,注意这个地方的横向单词指从一个起始格开始一直往右或者往下,直到遇见黑格子或者出界,并且每个字母在找横向或者竖向单词时只能用一

次 ,所以分为两个步骤,横向找和竖向找,每次判断该格子是不是起始格,并且是否在前面的单词中用过,若没用过就找下去,用过了则继续往下找起始格。

#include <iostream>
#include <cstdio>
#include <string.h>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <math.h>
#include <vector>
#include <set>
#define from(i,a,n)   for(int i=a;i<n;i++)
#define refrom(i,n,a) for(int i=n;i>=a;i--)
#define EPS 1e-10
#define mod 1000000007
using namespace std;

const double INF=0x3f3f3f3f;
const int MAX =11;
char puzzle[MAX][MAX],record[MAX][MAX];//这里的record数组用来记录起始格是否已经用于构成单词
int r,c,pos;

void across()
{
    int cnt=0;
    memset(record,0,sizeof(record));
    printf("Across\n");
    from(i,0,r)
    {
        from(j,0,c)//从左往右,从上往下扫描
        {
            int y=j;
            if(i==0&&puzzle[i][j]!='*')//第一行所有不是黑格子的都是起始格
            {
                cnt++;
                if(!record[i][j]) printf("%3d.",cnt);//如果该起始格没有用过,则一定可以找到一个单词
                else continue;
                while(puzzle[i][y]!='*'&&y<c)//往右输出,一直到遇见黑格子或者越界
                {
                    if(!record[i][y])
                    {
                        printf("%c",puzzle[i][y]);
                        record[i][y++]=1;
                    }
                    else break;
                }
                printf("\n");
                continue;
            }
            if(j==0&&puzzle[i][j]!='*')//第一列所有不是黑格子的但是起始格
            {
                cnt++;
                if(!record[i][j]) printf("%3d.",cnt);
                else continue;
                while(puzzle[i][y]!='*'&&y<c)
                {
                    if(!record[i][y])
                    {
                        printf("%c",puzzle[i][y]);
                        record[i][y++]=1;
                    }
                    else break;
                }
                printf("\n");
                continue;
            }
            if(puzzle[i][j]!='*'&&(puzzle[i][j-1]=='*'||puzzle[i-1][j]=='*'))//不是黑格子并且左边或者上面是黑格子的
            {
                cnt++;
                if(!record[i][j]) printf("%3d.",cnt);
                else continue;
                while(puzzle[i][y]!='*'&&y<c)
                {
                    if(!record[i][y])
                    {
                        printf("%c",puzzle[i][y]);
                        record[i][y++]=1;
                    }
                    else break;
                }
                printf("\n");
                continue;
            }
        }
    }
    return ;
}

void down()
{
    int cnt=0;
    memset(record,0,sizeof(record));
    printf("Down\n");
    from(i,0,r)
    {
        from(j,0,c)
        {
            int x=i;
            if(i==0&&puzzle[i][j]!='*')
            {
                cnt++;
                if(!record[i][j]) printf("%3d.",cnt);
                else continue;
                while(puzzle[x][j]!='*'&&x<r)
                {
                    if(!record[x][j])
                    {
                        printf("%c",puzzle[x][j]);
                        record[x++][j]=1;
                    }
                    else break;
                }
                printf("\n");
                continue;
            }
            if(j==0&&puzzle[i][j]!='*')
            {
                cnt++;
                if(!record[i][j]) printf("%3d.",cnt);
                else continue;
                while(puzzle[x][j]!='*'&&x<r)
                {
                    if(!record[x][j])
                    {
                        printf("%c",puzzle[x][j]);
                        record[x++][j]=1;
                    }
                    else break;
                }
                printf("\n");
                continue;
            }
             if(puzzle[i][j]!='*'&&(puzzle[i][j-1]=='*'||puzzle[i-1][j]=='*'))
            {
                cnt++;
                if(!record[i][j]) printf("%3d.",cnt);
                else continue;
                while(puzzle[x][j]!='*'&&x<r)
                {
                    if(!record[x][j])
                    {
                        printf("%c",puzzle[x][j]);
                        record[x++][j]=1;
                    }
                    else break;
                }
                printf("\n");
                continue;
            }
        }
    }
    return ;
}

int main()
{
    int t=0;
    while(cin>>r)
    {
        if(r==0) break;
        cin>>c;
        getchar();
        from(i,0,r)
        gets(puzzle[i]);
        if(t) printf("\n");
        printf("puzzle #%d:\n",++t);
        across();//横向找
        down();//竖向找
    }
    return 0;
}

具体代码如下:

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-30 16:55:53

uva--232(字符串模拟)的相关文章

UVa 232 字符串处理、

背景:做了三个半小时,代码能力堪忧啊,各种调试,各种出错,要分析一下,这些错点尽量不能再错. 学习:1.对于字符串数组,要把每一行都开大一位,该位用来存放'\0',否则将会出现未知输出.也就是说:字符串二维数组的每一行都可以看做一个字符数组,结尾都有一个'\0'.printf在用'%s'格式符输出字符串,总是从给定的首地址开始,遇到'\0'结束. 2.写程序的时候要有动态的眼光来看待当前写下的代码运行时的样子.运行出错不要理解单步调试,因先猜测是哪里错了,先看代码在脑中模拟. #include<

UVA 706 LCD Display 液晶显示屏 (字符串模拟)

[题目链接]click here~~ [题目大意] 给定的数字序列,按照要求输出对应液晶显示屏上的数字 输入: 2 12345 3 67890 0 0 输出: -- -- -- | | | | | | | | | | | | -- -- -- -- | | | | | | | | | | -- -- -- --- --- --- --- --- | | | | | | | | | | | | | | | | | | | | | | | | --- --- --- | | | | | | | |

UVA 10815-Andy&#39;s First Dictionary(字符串模拟+排序+重复删除)

Andy's First Dictionary Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description Problem B: Andy's First Dictionary Time limit: 3 seconds Andy, 8, has a dream - he wants to produce his very own dictionary. This

uva--1368(贪心,字符串模拟)

点击打开链接 该题是一个带有贪心思想的字符串模拟题,题目给定m个长度为n的字符串,让你求一个长度为n的字符串,使得该字符串与这m个字符串对应位置的字符不同的个数和最小. 要使对应位置不同字符最少,则该字符串每个字符优先选择该位置出现次数多的字符,若次数相同则选择字典序更小的字符. 代码: #include <iostream> #include <cstdio> #include <string.h> #include <map> #include <

hdu 4119 Isabella&#39;s Message【字符串模拟】

题目链接:http://write.blog.csdn.net/postedit 自我感觉比较麻烦 #include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> #include<string> #include<map> using namespace std; const int maxh=100+10; const int maxe=100

UVA 712(二叉树模拟)

L - S-Trees Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Appoint description:  System Crawler  (2014-04-01) Description  S-Trees  A Strange Tree (S-tree) over the variable set  is a binary tree representing a B

大数运算之字符串模拟

相信大家被特别大的两个数据做运算折磨过.当两个操作数或者运算结果超过类型的表示范围后会有意想不到的错误,这时候我们的电脑还不如我们高中用过的科学计算器,这是作为一个程序员所不能忍受的.所以我们得找到其他的方式来计算.这就是我们今天要讨论的字符串模拟大数运算. 我们的运算一般使用int类型来算的,那么首先我们先复习一下各种int类型的数据表示范围: unsigned int 0-4294967295    int   -2147483648-2147483647  unsigned long 0-

从1打印到最大的n位数字(字符串模拟数字自加)

陷阱:  用最大的n位数-1(数字太大可能产生越界) 应该采用字符串模拟数字自加! 代码如下: #include<iostream> using namespace std; int  IsMax(char *number) {  int nLength = strlen(number);  int CarryBit = 0;  bool  ret = false;  for (int i = nLength-1; i >= 0; i--)  {   int nSum = number[

【002】}链表或字符串模拟加法/加一/乘法

链表模拟加法/字符串模拟二进制加法/数组模拟加一操作/打印1到最大的n位数/字符串模拟乘法 ============================================ Add Two Numbers 两个链表代表两个数字,每个结点的值都是一位数字,单链表逆序存放这两个数字, 构造出一个新的链表,代表这两个链表的和. 链表的尾插法,头结点dummy结点的运用,统一对prev指针的操作, C++ Code 1234567891011121314151617181920212223242