File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Parsing M*N matrix on Periphery Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Parsing M*N matrix on Periphery" Watch "Parsing M*N matrix on Periphery" New topic
Author

Parsing M*N matrix on Periphery

Amarbir Singh
Greenhorn

Joined: Jun 26, 2007
Posts: 20
Hi All,

I have this query of parsing an M*N matrix on periphery...
e.g we are given 4*4 matrix as below...

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

We need to parse it to display output in the sequence as shown....

1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10.

Please guide me to write the algorithm generic for the same....!

Thank you very much.
cheers
Amar
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Presumably, this is a homework assignment. I hope no-one's going to do your homework for you. But, if you show how you've tried, and where you're stuck, we're happy to help.

I don't know if there's a "clever" solution to this problem. I'd do it the "dumb" way, with 4 loops. Does that help you to get started?

(Please don't anyone post full code for their "clever" or "dumb" solution!)


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

The recursive solution might be considered "clever".
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
How would you tell somebody who doesn't know the rules how to move a coin around a chess board in this pattern? "go east, go east, go south ..."

How would you describe the rules? Seriously, see if you can write it out in plain English first. Show us what you get!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Amarbir Singh
Greenhorn

Joined: Jun 26, 2007
Posts: 20
Hi Frnz..Thank you very much....for your guidance..
what i was looking for ... Is there any mathematical algorithm for handling M*N matrix ... to parse it on the periphery...!

or do we need to go through 4-different loops.....?

thnks in advance...!
cheers

" Art of people is the true mirror of their minds...! "
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Um, the mathematical algorithm for solving this will probably involve four inner loops inside one outer loop. Does that answer your question?


"I'm not back." - Bill Harding, Twister
Amarbir Singh
Greenhorn

Joined: Jun 26, 2007
Posts: 20
I wrote following two functions to increment/decrement based on flag...
now the issue is how to call these symmetrically in loop so that parameter values are correctly passed.

Is there any symmetry that i can follow in loop to call these functions for desired output.


[ added code tags - Jim ]
[ October 05, 2007: Message edited by: Jim Yingst ]
Amarbir Singh
Greenhorn

Joined: Jun 26, 2007
Posts: 20
I'm using two variables to control the number of iterations in the while loop that gets changed when we do i--/j--/j++ etc....
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I did something in Pascal once with "delta pairs" that tell how to adjust X & Y to go each direction. With 0,0 in the top left corner ...

EAST: +1, 0
SOUTH: 0, +1
WEST: -1, 0
NORTH: 0, -1

Now we can do it in one loop, I think:

That's totally untested. Surely one could clean it up to eliminate the double "next cell" thingy. Do you think it would work?
[ October 06, 2007: Message edited by: Stan James ]
Amarbir Singh
Greenhorn

Joined: Jun 26, 2007
Posts: 20
Thanks James,

Your algorithm will be of great help. Marking the nodes as Visited/Unvisited should help make it work perfectly fine.

Thanks everybody for your guidance.

"The Art Of People Is The True Mirror Of Their Minds."
 
Don't get me started about those stupid light bulbs.
 
subject: Parsing M*N matrix on Periphery